block.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091
  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/cdev.h>
  31. #include <linux/mutex.h>
  32. #include <linux/scatterlist.h>
  33. #include <linux/string_helpers.h>
  34. #include <linux/delay.h>
  35. #include <linux/capability.h>
  36. #include <linux/compat.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/idr.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/mmc/ioctl.h>
  41. #include <linux/mmc/card.h>
  42. #include <linux/mmc/host.h>
  43. #include <linux/mmc/mmc.h>
  44. #include <linux/mmc/sd.h>
  45. #include <linux/uaccess.h>
  46. #include "queue.h"
  47. #include "block.h"
  48. #include "core.h"
  49. #include "card.h"
  50. #include "host.h"
  51. #include "bus.h"
  52. #include "mmc_ops.h"
  53. #include "quirks.h"
  54. #include "sd_ops.h"
  55. MODULE_ALIAS("mmc:block");
  56. #ifdef MODULE_PARAM_PREFIX
  57. #undef MODULE_PARAM_PREFIX
  58. #endif
  59. #define MODULE_PARAM_PREFIX "mmcblk."
  60. /*
  61. * Set a 10 second timeout for polling write request busy state. Note, mmc core
  62. * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
  63. * second software timer to timeout the whole request, so 10 seconds should be
  64. * ample.
  65. */
  66. #define MMC_BLK_TIMEOUT_MS (10 * 1000)
  67. #define MMC_SANITIZE_REQ_TIMEOUT 240000
  68. #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
  69. #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
  70. #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
  71. (rq_data_dir(req) == WRITE))
  72. static DEFINE_MUTEX(block_mutex);
  73. /*
  74. * The defaults come from config options but can be overriden by module
  75. * or bootarg options.
  76. */
  77. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  78. /*
  79. * We've only got one major, so number of mmcblk devices is
  80. * limited to (1 << 20) / number of minors per device. It is also
  81. * limited by the MAX_DEVICES below.
  82. */
  83. static int max_devices;
  84. #define MAX_DEVICES 256
  85. static DEFINE_IDA(mmc_blk_ida);
  86. static DEFINE_IDA(mmc_rpmb_ida);
  87. /*
  88. * There is one mmc_blk_data per slot.
  89. */
  90. struct mmc_blk_data {
  91. spinlock_t lock;
  92. struct device *parent;
  93. struct gendisk *disk;
  94. struct mmc_queue queue;
  95. struct list_head part;
  96. struct list_head rpmbs;
  97. unsigned int flags;
  98. #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
  99. #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
  100. unsigned int usage;
  101. unsigned int read_only;
  102. unsigned int part_type;
  103. unsigned int reset_done;
  104. #define MMC_BLK_READ BIT(0)
  105. #define MMC_BLK_WRITE BIT(1)
  106. #define MMC_BLK_DISCARD BIT(2)
  107. #define MMC_BLK_SECDISCARD BIT(3)
  108. #define MMC_BLK_CQE_RECOVERY BIT(4)
  109. /*
  110. * Only set in main mmc_blk_data associated
  111. * with mmc_card with dev_set_drvdata, and keeps
  112. * track of the current selected device partition.
  113. */
  114. unsigned int part_curr;
  115. struct device_attribute force_ro;
  116. struct device_attribute power_ro_lock;
  117. int area_type;
  118. /* debugfs files (only in main mmc_blk_data) */
  119. struct dentry *status_dentry;
  120. struct dentry *ext_csd_dentry;
  121. };
  122. /* Device type for RPMB character devices */
  123. static dev_t mmc_rpmb_devt;
  124. /* Bus type for RPMB character devices */
  125. static struct bus_type mmc_rpmb_bus_type = {
  126. .name = "mmc_rpmb",
  127. };
  128. /**
  129. * struct mmc_rpmb_data - special RPMB device type for these areas
  130. * @dev: the device for the RPMB area
  131. * @chrdev: character device for the RPMB area
  132. * @id: unique device ID number
  133. * @part_index: partition index (0 on first)
  134. * @md: parent MMC block device
  135. * @node: list item, so we can put this device on a list
  136. */
  137. struct mmc_rpmb_data {
  138. struct device dev;
  139. struct cdev chrdev;
  140. int id;
  141. unsigned int part_index;
  142. struct mmc_blk_data *md;
  143. struct list_head node;
  144. };
  145. static DEFINE_MUTEX(open_lock);
  146. module_param(perdev_minors, int, 0444);
  147. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  148. static inline int mmc_blk_part_switch(struct mmc_card *card,
  149. unsigned int part_type);
  150. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  151. {
  152. struct mmc_blk_data *md;
  153. mutex_lock(&open_lock);
  154. md = disk->private_data;
  155. if (md && md->usage == 0)
  156. md = NULL;
  157. if (md)
  158. md->usage++;
  159. mutex_unlock(&open_lock);
  160. return md;
  161. }
  162. static inline int mmc_get_devidx(struct gendisk *disk)
  163. {
  164. int devidx = disk->first_minor / perdev_minors;
  165. return devidx;
  166. }
  167. static void mmc_blk_put(struct mmc_blk_data *md)
  168. {
  169. mutex_lock(&open_lock);
  170. md->usage--;
  171. if (md->usage == 0) {
  172. int devidx = mmc_get_devidx(md->disk);
  173. blk_put_queue(md->queue.queue);
  174. ida_simple_remove(&mmc_blk_ida, devidx);
  175. put_disk(md->disk);
  176. kfree(md);
  177. }
  178. mutex_unlock(&open_lock);
  179. }
  180. static ssize_t power_ro_lock_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. int ret;
  184. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  185. struct mmc_card *card = md->queue.card;
  186. int locked = 0;
  187. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
  188. locked = 2;
  189. else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
  190. locked = 1;
  191. ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
  192. mmc_blk_put(md);
  193. return ret;
  194. }
  195. static ssize_t power_ro_lock_store(struct device *dev,
  196. struct device_attribute *attr, const char *buf, size_t count)
  197. {
  198. int ret;
  199. struct mmc_blk_data *md, *part_md;
  200. struct mmc_queue *mq;
  201. struct request *req;
  202. unsigned long set;
  203. if (kstrtoul(buf, 0, &set))
  204. return -EINVAL;
  205. if (set != 1)
  206. return count;
  207. md = mmc_blk_get(dev_to_disk(dev));
  208. mq = &md->queue;
  209. /* Dispatch locking to the block layer */
  210. req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
  211. if (IS_ERR(req)) {
  212. count = PTR_ERR(req);
  213. goto out_put;
  214. }
  215. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
  216. blk_execute_rq(mq->queue, NULL, req, 0);
  217. ret = req_to_mmc_queue_req(req)->drv_op_result;
  218. blk_put_request(req);
  219. if (!ret) {
  220. pr_info("%s: Locking boot partition ro until next power on\n",
  221. md->disk->disk_name);
  222. set_disk_ro(md->disk, 1);
  223. list_for_each_entry(part_md, &md->part, part)
  224. if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
  225. pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
  226. set_disk_ro(part_md->disk, 1);
  227. }
  228. }
  229. out_put:
  230. mmc_blk_put(md);
  231. return count;
  232. }
  233. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  234. char *buf)
  235. {
  236. int ret;
  237. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  238. ret = snprintf(buf, PAGE_SIZE, "%d\n",
  239. get_disk_ro(dev_to_disk(dev)) ^
  240. md->read_only);
  241. mmc_blk_put(md);
  242. return ret;
  243. }
  244. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. int ret;
  248. char *end;
  249. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  250. unsigned long set = simple_strtoul(buf, &end, 0);
  251. if (end == buf) {
  252. ret = -EINVAL;
  253. goto out;
  254. }
  255. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  256. ret = count;
  257. out:
  258. mmc_blk_put(md);
  259. return ret;
  260. }
  261. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  262. {
  263. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  264. int ret = -ENXIO;
  265. mutex_lock(&block_mutex);
  266. if (md) {
  267. if (md->usage == 2)
  268. check_disk_change(bdev);
  269. ret = 0;
  270. if ((mode & FMODE_WRITE) && md->read_only) {
  271. mmc_blk_put(md);
  272. ret = -EROFS;
  273. }
  274. }
  275. mutex_unlock(&block_mutex);
  276. return ret;
  277. }
  278. static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
  279. {
  280. struct mmc_blk_data *md = disk->private_data;
  281. mutex_lock(&block_mutex);
  282. mmc_blk_put(md);
  283. mutex_unlock(&block_mutex);
  284. }
  285. static int
  286. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  287. {
  288. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  289. geo->heads = 4;
  290. geo->sectors = 16;
  291. return 0;
  292. }
  293. struct mmc_blk_ioc_data {
  294. struct mmc_ioc_cmd ic;
  295. unsigned char *buf;
  296. u64 buf_bytes;
  297. struct mmc_rpmb_data *rpmb;
  298. };
  299. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  300. struct mmc_ioc_cmd __user *user)
  301. {
  302. struct mmc_blk_ioc_data *idata;
  303. int err;
  304. idata = kmalloc(sizeof(*idata), GFP_KERNEL);
  305. if (!idata) {
  306. err = -ENOMEM;
  307. goto out;
  308. }
  309. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  310. err = -EFAULT;
  311. goto idata_err;
  312. }
  313. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  314. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  315. err = -EOVERFLOW;
  316. goto idata_err;
  317. }
  318. if (!idata->buf_bytes) {
  319. idata->buf = NULL;
  320. return idata;
  321. }
  322. idata->buf = memdup_user((void __user *)(unsigned long)
  323. idata->ic.data_ptr, idata->buf_bytes);
  324. if (IS_ERR(idata->buf)) {
  325. err = PTR_ERR(idata->buf);
  326. goto idata_err;
  327. }
  328. return idata;
  329. idata_err:
  330. kfree(idata);
  331. out:
  332. return ERR_PTR(err);
  333. }
  334. static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
  335. struct mmc_blk_ioc_data *idata)
  336. {
  337. struct mmc_ioc_cmd *ic = &idata->ic;
  338. if (copy_to_user(&(ic_ptr->response), ic->response,
  339. sizeof(ic->response)))
  340. return -EFAULT;
  341. if (!idata->ic.write_flag) {
  342. if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
  343. idata->buf, idata->buf_bytes))
  344. return -EFAULT;
  345. }
  346. return 0;
  347. }
  348. static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
  349. u32 retries_max)
  350. {
  351. int err;
  352. u32 retry_count = 0;
  353. if (!status || !retries_max)
  354. return -EINVAL;
  355. do {
  356. err = __mmc_send_status(card, status, 5);
  357. if (err)
  358. break;
  359. if (!R1_STATUS(*status) &&
  360. (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
  361. break; /* RPMB programming operation complete */
  362. /*
  363. * Rechedule to give the MMC device a chance to continue
  364. * processing the previous command without being polled too
  365. * frequently.
  366. */
  367. usleep_range(1000, 5000);
  368. } while (++retry_count < retries_max);
  369. if (retry_count == retries_max)
  370. err = -EPERM;
  371. return err;
  372. }
  373. static int ioctl_do_sanitize(struct mmc_card *card)
  374. {
  375. int err;
  376. if (!mmc_can_sanitize(card)) {
  377. pr_warn("%s: %s - SANITIZE is not supported\n",
  378. mmc_hostname(card->host), __func__);
  379. err = -EOPNOTSUPP;
  380. goto out;
  381. }
  382. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  383. mmc_hostname(card->host), __func__);
  384. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  385. EXT_CSD_SANITIZE_START, 1,
  386. MMC_SANITIZE_REQ_TIMEOUT);
  387. if (err)
  388. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  389. mmc_hostname(card->host), __func__, err);
  390. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  391. __func__);
  392. out:
  393. return err;
  394. }
  395. static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
  396. struct mmc_blk_ioc_data *idata)
  397. {
  398. struct mmc_command cmd = {};
  399. struct mmc_data data = {};
  400. struct mmc_request mrq = {};
  401. struct scatterlist sg;
  402. int err;
  403. unsigned int target_part;
  404. u32 status = 0;
  405. if (!card || !md || !idata)
  406. return -EINVAL;
  407. /*
  408. * The RPMB accesses comes in from the character device, so we
  409. * need to target these explicitly. Else we just target the
  410. * partition type for the block device the ioctl() was issued
  411. * on.
  412. */
  413. if (idata->rpmb) {
  414. /* Support multiple RPMB partitions */
  415. target_part = idata->rpmb->part_index;
  416. target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
  417. } else {
  418. target_part = md->part_type;
  419. }
  420. cmd.opcode = idata->ic.opcode;
  421. cmd.arg = idata->ic.arg;
  422. cmd.flags = idata->ic.flags;
  423. if (idata->buf_bytes) {
  424. data.sg = &sg;
  425. data.sg_len = 1;
  426. data.blksz = idata->ic.blksz;
  427. data.blocks = idata->ic.blocks;
  428. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  429. if (idata->ic.write_flag)
  430. data.flags = MMC_DATA_WRITE;
  431. else
  432. data.flags = MMC_DATA_READ;
  433. /* data.flags must already be set before doing this. */
  434. mmc_set_data_timeout(&data, card);
  435. /* Allow overriding the timeout_ns for empirical tuning. */
  436. if (idata->ic.data_timeout_ns)
  437. data.timeout_ns = idata->ic.data_timeout_ns;
  438. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  439. /*
  440. * Pretend this is a data transfer and rely on the
  441. * host driver to compute timeout. When all host
  442. * drivers support cmd.cmd_timeout for R1B, this
  443. * can be changed to:
  444. *
  445. * mrq.data = NULL;
  446. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  447. */
  448. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  449. }
  450. mrq.data = &data;
  451. }
  452. mrq.cmd = &cmd;
  453. err = mmc_blk_part_switch(card, target_part);
  454. if (err)
  455. return err;
  456. if (idata->ic.is_acmd) {
  457. err = mmc_app_cmd(card->host, card);
  458. if (err)
  459. return err;
  460. }
  461. if (idata->rpmb) {
  462. err = mmc_set_blockcount(card, data.blocks,
  463. idata->ic.write_flag & (1 << 31));
  464. if (err)
  465. return err;
  466. }
  467. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
  468. (cmd.opcode == MMC_SWITCH)) {
  469. err = ioctl_do_sanitize(card);
  470. if (err)
  471. pr_err("%s: ioctl_do_sanitize() failed. err = %d",
  472. __func__, err);
  473. return err;
  474. }
  475. mmc_wait_for_req(card->host, &mrq);
  476. if (cmd.error) {
  477. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  478. __func__, cmd.error);
  479. return cmd.error;
  480. }
  481. if (data.error) {
  482. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  483. __func__, data.error);
  484. return data.error;
  485. }
  486. /*
  487. * Make sure the cache of the PARTITION_CONFIG register and
  488. * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
  489. * changed it successfully.
  490. */
  491. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
  492. (cmd.opcode == MMC_SWITCH)) {
  493. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  494. u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
  495. /*
  496. * Update cache so the next mmc_blk_part_switch call operates
  497. * on up-to-date data.
  498. */
  499. card->ext_csd.part_config = value;
  500. main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
  501. }
  502. /*
  503. * According to the SD specs, some commands require a delay after
  504. * issuing the command.
  505. */
  506. if (idata->ic.postsleep_min_us)
  507. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  508. memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
  509. if (idata->rpmb) {
  510. /*
  511. * Ensure RPMB command has completed by polling CMD13
  512. * "Send Status".
  513. */
  514. err = ioctl_rpmb_card_status_poll(card, &status, 5);
  515. if (err)
  516. dev_err(mmc_dev(card->host),
  517. "%s: Card Status=0x%08X, error %d\n",
  518. __func__, status, err);
  519. }
  520. return err;
  521. }
  522. static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
  523. struct mmc_ioc_cmd __user *ic_ptr,
  524. struct mmc_rpmb_data *rpmb)
  525. {
  526. struct mmc_blk_ioc_data *idata;
  527. struct mmc_blk_ioc_data *idatas[1];
  528. struct mmc_queue *mq;
  529. struct mmc_card *card;
  530. int err = 0, ioc_err = 0;
  531. struct request *req;
  532. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  533. if (IS_ERR(idata))
  534. return PTR_ERR(idata);
  535. /* This will be NULL on non-RPMB ioctl():s */
  536. idata->rpmb = rpmb;
  537. card = md->queue.card;
  538. if (IS_ERR(card)) {
  539. err = PTR_ERR(card);
  540. goto cmd_done;
  541. }
  542. /*
  543. * Dispatch the ioctl() into the block request queue.
  544. */
  545. mq = &md->queue;
  546. req = blk_get_request(mq->queue,
  547. idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
  548. __GFP_RECLAIM);
  549. if (IS_ERR(req)) {
  550. err = PTR_ERR(req);
  551. goto cmd_done;
  552. }
  553. idatas[0] = idata;
  554. req_to_mmc_queue_req(req)->drv_op =
  555. rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
  556. req_to_mmc_queue_req(req)->drv_op_data = idatas;
  557. req_to_mmc_queue_req(req)->ioc_count = 1;
  558. blk_execute_rq(mq->queue, NULL, req, 0);
  559. ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
  560. err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
  561. blk_put_request(req);
  562. cmd_done:
  563. kfree(idata->buf);
  564. kfree(idata);
  565. return ioc_err ? ioc_err : err;
  566. }
  567. static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
  568. struct mmc_ioc_multi_cmd __user *user,
  569. struct mmc_rpmb_data *rpmb)
  570. {
  571. struct mmc_blk_ioc_data **idata = NULL;
  572. struct mmc_ioc_cmd __user *cmds = user->cmds;
  573. struct mmc_card *card;
  574. struct mmc_queue *mq;
  575. int i, err = 0, ioc_err = 0;
  576. __u64 num_of_cmds;
  577. struct request *req;
  578. if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
  579. sizeof(num_of_cmds)))
  580. return -EFAULT;
  581. if (!num_of_cmds)
  582. return 0;
  583. if (num_of_cmds > MMC_IOC_MAX_CMDS)
  584. return -EINVAL;
  585. idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
  586. if (!idata)
  587. return -ENOMEM;
  588. for (i = 0; i < num_of_cmds; i++) {
  589. idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
  590. if (IS_ERR(idata[i])) {
  591. err = PTR_ERR(idata[i]);
  592. num_of_cmds = i;
  593. goto cmd_err;
  594. }
  595. /* This will be NULL on non-RPMB ioctl():s */
  596. idata[i]->rpmb = rpmb;
  597. }
  598. card = md->queue.card;
  599. if (IS_ERR(card)) {
  600. err = PTR_ERR(card);
  601. goto cmd_err;
  602. }
  603. /*
  604. * Dispatch the ioctl()s into the block request queue.
  605. */
  606. mq = &md->queue;
  607. req = blk_get_request(mq->queue,
  608. idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
  609. __GFP_RECLAIM);
  610. if (IS_ERR(req)) {
  611. err = PTR_ERR(req);
  612. goto cmd_err;
  613. }
  614. req_to_mmc_queue_req(req)->drv_op =
  615. rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
  616. req_to_mmc_queue_req(req)->drv_op_data = idata;
  617. req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
  618. blk_execute_rq(mq->queue, NULL, req, 0);
  619. ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
  620. /* copy to user if data and response */
  621. for (i = 0; i < num_of_cmds && !err; i++)
  622. err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
  623. blk_put_request(req);
  624. cmd_err:
  625. for (i = 0; i < num_of_cmds; i++) {
  626. kfree(idata[i]->buf);
  627. kfree(idata[i]);
  628. }
  629. kfree(idata);
  630. return ioc_err ? ioc_err : err;
  631. }
  632. static int mmc_blk_check_blkdev(struct block_device *bdev)
  633. {
  634. /*
  635. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  636. * whole block device, not on a partition. This prevents overspray
  637. * between sibling partitions.
  638. */
  639. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  640. return -EPERM;
  641. return 0;
  642. }
  643. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  644. unsigned int cmd, unsigned long arg)
  645. {
  646. struct mmc_blk_data *md;
  647. int ret;
  648. switch (cmd) {
  649. case MMC_IOC_CMD:
  650. ret = mmc_blk_check_blkdev(bdev);
  651. if (ret)
  652. return ret;
  653. md = mmc_blk_get(bdev->bd_disk);
  654. if (!md)
  655. return -EINVAL;
  656. ret = mmc_blk_ioctl_cmd(md,
  657. (struct mmc_ioc_cmd __user *)arg,
  658. NULL);
  659. mmc_blk_put(md);
  660. return ret;
  661. case MMC_IOC_MULTI_CMD:
  662. ret = mmc_blk_check_blkdev(bdev);
  663. if (ret)
  664. return ret;
  665. md = mmc_blk_get(bdev->bd_disk);
  666. if (!md)
  667. return -EINVAL;
  668. ret = mmc_blk_ioctl_multi_cmd(md,
  669. (struct mmc_ioc_multi_cmd __user *)arg,
  670. NULL);
  671. mmc_blk_put(md);
  672. return ret;
  673. default:
  674. return -EINVAL;
  675. }
  676. }
  677. #ifdef CONFIG_COMPAT
  678. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  679. unsigned int cmd, unsigned long arg)
  680. {
  681. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  682. }
  683. #endif
  684. static const struct block_device_operations mmc_bdops = {
  685. .open = mmc_blk_open,
  686. .release = mmc_blk_release,
  687. .getgeo = mmc_blk_getgeo,
  688. .owner = THIS_MODULE,
  689. .ioctl = mmc_blk_ioctl,
  690. #ifdef CONFIG_COMPAT
  691. .compat_ioctl = mmc_blk_compat_ioctl,
  692. #endif
  693. };
  694. static int mmc_blk_part_switch_pre(struct mmc_card *card,
  695. unsigned int part_type)
  696. {
  697. int ret = 0;
  698. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  699. if (card->ext_csd.cmdq_en) {
  700. ret = mmc_cmdq_disable(card);
  701. if (ret)
  702. return ret;
  703. }
  704. mmc_retune_pause(card->host);
  705. }
  706. return ret;
  707. }
  708. static int mmc_blk_part_switch_post(struct mmc_card *card,
  709. unsigned int part_type)
  710. {
  711. int ret = 0;
  712. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  713. mmc_retune_unpause(card->host);
  714. if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
  715. ret = mmc_cmdq_enable(card);
  716. }
  717. return ret;
  718. }
  719. static inline int mmc_blk_part_switch(struct mmc_card *card,
  720. unsigned int part_type)
  721. {
  722. int ret = 0;
  723. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  724. if (main_md->part_curr == part_type)
  725. return 0;
  726. if (mmc_card_mmc(card)) {
  727. u8 part_config = card->ext_csd.part_config;
  728. ret = mmc_blk_part_switch_pre(card, part_type);
  729. if (ret)
  730. return ret;
  731. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  732. part_config |= part_type;
  733. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  734. EXT_CSD_PART_CONFIG, part_config,
  735. card->ext_csd.part_time);
  736. if (ret) {
  737. mmc_blk_part_switch_post(card, part_type);
  738. return ret;
  739. }
  740. card->ext_csd.part_config = part_config;
  741. ret = mmc_blk_part_switch_post(card, main_md->part_curr);
  742. }
  743. main_md->part_curr = part_type;
  744. return ret;
  745. }
  746. static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
  747. {
  748. int err;
  749. u32 result;
  750. __be32 *blocks;
  751. struct mmc_request mrq = {};
  752. struct mmc_command cmd = {};
  753. struct mmc_data data = {};
  754. struct scatterlist sg;
  755. cmd.opcode = MMC_APP_CMD;
  756. cmd.arg = card->rca << 16;
  757. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  758. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  759. if (err)
  760. return err;
  761. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  762. return -EIO;
  763. memset(&cmd, 0, sizeof(struct mmc_command));
  764. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  765. cmd.arg = 0;
  766. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  767. data.blksz = 4;
  768. data.blocks = 1;
  769. data.flags = MMC_DATA_READ;
  770. data.sg = &sg;
  771. data.sg_len = 1;
  772. mmc_set_data_timeout(&data, card);
  773. mrq.cmd = &cmd;
  774. mrq.data = &data;
  775. blocks = kmalloc(4, GFP_KERNEL);
  776. if (!blocks)
  777. return -ENOMEM;
  778. sg_init_one(&sg, blocks, 4);
  779. mmc_wait_for_req(card->host, &mrq);
  780. result = ntohl(*blocks);
  781. kfree(blocks);
  782. if (cmd.error || data.error)
  783. return -EIO;
  784. *written_blocks = result;
  785. return 0;
  786. }
  787. static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
  788. {
  789. if (host->actual_clock)
  790. return host->actual_clock / 1000;
  791. /* Clock may be subject to a divisor, fudge it by a factor of 2. */
  792. if (host->ios.clock)
  793. return host->ios.clock / 2000;
  794. /* How can there be no clock */
  795. WARN_ON_ONCE(1);
  796. return 100; /* 100 kHz is minimum possible value */
  797. }
  798. static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
  799. struct mmc_data *data)
  800. {
  801. unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
  802. unsigned int khz;
  803. if (data->timeout_clks) {
  804. khz = mmc_blk_clock_khz(host);
  805. ms += DIV_ROUND_UP(data->timeout_clks, khz);
  806. }
  807. return ms;
  808. }
  809. static inline bool mmc_blk_in_tran_state(u32 status)
  810. {
  811. /*
  812. * Some cards mishandle the status bits, so make sure to check both the
  813. * busy indication and the card state.
  814. */
  815. return status & R1_READY_FOR_DATA &&
  816. (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
  817. }
  818. static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
  819. struct request *req, u32 *resp_errs)
  820. {
  821. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  822. int err = 0;
  823. u32 status;
  824. do {
  825. bool done = time_after(jiffies, timeout);
  826. err = __mmc_send_status(card, &status, 5);
  827. if (err) {
  828. pr_err("%s: error %d requesting status\n",
  829. req->rq_disk->disk_name, err);
  830. return err;
  831. }
  832. /* Accumulate any response error bits seen */
  833. if (resp_errs)
  834. *resp_errs |= status;
  835. /*
  836. * Timeout if the device never becomes ready for data and never
  837. * leaves the program state.
  838. */
  839. if (done) {
  840. pr_err("%s: Card stuck in wrong state! %s %s status: %#x\n",
  841. mmc_hostname(card->host),
  842. req->rq_disk->disk_name, __func__, status);
  843. return -ETIMEDOUT;
  844. }
  845. /*
  846. * Some cards mishandle the status bits,
  847. * so make sure to check both the busy
  848. * indication and the card state.
  849. */
  850. } while (!mmc_blk_in_tran_state(status));
  851. return err;
  852. }
  853. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  854. int type)
  855. {
  856. int err;
  857. if (md->reset_done & type)
  858. return -EEXIST;
  859. md->reset_done |= type;
  860. err = mmc_hw_reset(host);
  861. /* Ensure we switch back to the correct partition */
  862. if (err != -EOPNOTSUPP) {
  863. struct mmc_blk_data *main_md =
  864. dev_get_drvdata(&host->card->dev);
  865. int part_err;
  866. main_md->part_curr = main_md->part_type;
  867. part_err = mmc_blk_part_switch(host->card, md->part_type);
  868. if (part_err) {
  869. /*
  870. * We have failed to get back into the correct
  871. * partition, so we need to abort the whole request.
  872. */
  873. return -ENODEV;
  874. }
  875. }
  876. return err;
  877. }
  878. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  879. {
  880. md->reset_done &= ~type;
  881. }
  882. /*
  883. * The non-block commands come back from the block layer after it queued it and
  884. * processed it with all other requests and then they get issued in this
  885. * function.
  886. */
  887. static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
  888. {
  889. struct mmc_queue_req *mq_rq;
  890. struct mmc_card *card = mq->card;
  891. struct mmc_blk_data *md = mq->blkdata;
  892. struct mmc_blk_ioc_data **idata;
  893. bool rpmb_ioctl;
  894. u8 **ext_csd;
  895. u32 status;
  896. int ret;
  897. int i;
  898. mq_rq = req_to_mmc_queue_req(req);
  899. rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
  900. switch (mq_rq->drv_op) {
  901. case MMC_DRV_OP_IOCTL:
  902. case MMC_DRV_OP_IOCTL_RPMB:
  903. idata = mq_rq->drv_op_data;
  904. for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
  905. ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
  906. if (ret)
  907. break;
  908. }
  909. /* Always switch back to main area after RPMB access */
  910. if (rpmb_ioctl)
  911. mmc_blk_part_switch(card, 0);
  912. break;
  913. case MMC_DRV_OP_BOOT_WP:
  914. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
  915. card->ext_csd.boot_ro_lock |
  916. EXT_CSD_BOOT_WP_B_PWR_WP_EN,
  917. card->ext_csd.part_time);
  918. if (ret)
  919. pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
  920. md->disk->disk_name, ret);
  921. else
  922. card->ext_csd.boot_ro_lock |=
  923. EXT_CSD_BOOT_WP_B_PWR_WP_EN;
  924. break;
  925. case MMC_DRV_OP_GET_CARD_STATUS:
  926. ret = mmc_send_status(card, &status);
  927. if (!ret)
  928. ret = status;
  929. break;
  930. case MMC_DRV_OP_GET_EXT_CSD:
  931. ext_csd = mq_rq->drv_op_data;
  932. ret = mmc_get_ext_csd(card, ext_csd);
  933. break;
  934. default:
  935. pr_err("%s: unknown driver specific operation\n",
  936. md->disk->disk_name);
  937. ret = -EINVAL;
  938. break;
  939. }
  940. mq_rq->drv_op_result = ret;
  941. blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
  942. }
  943. static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  944. {
  945. struct mmc_blk_data *md = mq->blkdata;
  946. struct mmc_card *card = md->queue.card;
  947. unsigned int from, nr, arg;
  948. int err = 0, type = MMC_BLK_DISCARD;
  949. blk_status_t status = BLK_STS_OK;
  950. if (!mmc_can_erase(card)) {
  951. status = BLK_STS_NOTSUPP;
  952. goto fail;
  953. }
  954. from = blk_rq_pos(req);
  955. nr = blk_rq_sectors(req);
  956. if (mmc_can_discard(card))
  957. arg = MMC_DISCARD_ARG;
  958. else if (mmc_can_trim(card))
  959. arg = MMC_TRIM_ARG;
  960. else
  961. arg = MMC_ERASE_ARG;
  962. do {
  963. err = 0;
  964. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  965. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  966. INAND_CMD38_ARG_EXT_CSD,
  967. arg == MMC_TRIM_ARG ?
  968. INAND_CMD38_ARG_TRIM :
  969. INAND_CMD38_ARG_ERASE,
  970. 0);
  971. }
  972. if (!err)
  973. err = mmc_erase(card, from, nr, arg);
  974. } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
  975. if (err)
  976. status = BLK_STS_IOERR;
  977. else
  978. mmc_blk_reset_success(md, type);
  979. fail:
  980. blk_mq_end_request(req, status);
  981. }
  982. static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  983. struct request *req)
  984. {
  985. struct mmc_blk_data *md = mq->blkdata;
  986. struct mmc_card *card = md->queue.card;
  987. unsigned int from, nr, arg;
  988. int err = 0, type = MMC_BLK_SECDISCARD;
  989. blk_status_t status = BLK_STS_OK;
  990. if (!(mmc_can_secure_erase_trim(card))) {
  991. status = BLK_STS_NOTSUPP;
  992. goto out;
  993. }
  994. from = blk_rq_pos(req);
  995. nr = blk_rq_sectors(req);
  996. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  997. arg = MMC_SECURE_TRIM1_ARG;
  998. else
  999. arg = MMC_SECURE_ERASE_ARG;
  1000. retry:
  1001. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1002. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1003. INAND_CMD38_ARG_EXT_CSD,
  1004. arg == MMC_SECURE_TRIM1_ARG ?
  1005. INAND_CMD38_ARG_SECTRIM1 :
  1006. INAND_CMD38_ARG_SECERASE,
  1007. 0);
  1008. if (err)
  1009. goto out_retry;
  1010. }
  1011. err = mmc_erase(card, from, nr, arg);
  1012. if (err == -EIO)
  1013. goto out_retry;
  1014. if (err) {
  1015. status = BLK_STS_IOERR;
  1016. goto out;
  1017. }
  1018. if (arg == MMC_SECURE_TRIM1_ARG) {
  1019. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1020. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1021. INAND_CMD38_ARG_EXT_CSD,
  1022. INAND_CMD38_ARG_SECTRIM2,
  1023. 0);
  1024. if (err)
  1025. goto out_retry;
  1026. }
  1027. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  1028. if (err == -EIO)
  1029. goto out_retry;
  1030. if (err) {
  1031. status = BLK_STS_IOERR;
  1032. goto out;
  1033. }
  1034. }
  1035. out_retry:
  1036. if (err && !mmc_blk_reset(md, card->host, type))
  1037. goto retry;
  1038. if (!err)
  1039. mmc_blk_reset_success(md, type);
  1040. out:
  1041. blk_mq_end_request(req, status);
  1042. }
  1043. static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  1044. {
  1045. struct mmc_blk_data *md = mq->blkdata;
  1046. struct mmc_card *card = md->queue.card;
  1047. int ret = 0;
  1048. ret = mmc_flush_cache(card);
  1049. blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
  1050. }
  1051. /*
  1052. * Reformat current write as a reliable write, supporting
  1053. * both legacy and the enhanced reliable write MMC cards.
  1054. * In each transfer we'll handle only as much as a single
  1055. * reliable write can handle, thus finish the request in
  1056. * partial completions.
  1057. */
  1058. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  1059. struct mmc_card *card,
  1060. struct request *req)
  1061. {
  1062. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  1063. /* Legacy mode imposes restrictions on transfers. */
  1064. if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
  1065. brq->data.blocks = 1;
  1066. if (brq->data.blocks > card->ext_csd.rel_sectors)
  1067. brq->data.blocks = card->ext_csd.rel_sectors;
  1068. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  1069. brq->data.blocks = 1;
  1070. }
  1071. }
  1072. #define CMD_ERRORS_EXCL_OOR \
  1073. (R1_ADDRESS_ERROR | /* Misaligned address */ \
  1074. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  1075. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  1076. R1_CARD_ECC_FAILED | /* Card ECC failed */ \
  1077. R1_CC_ERROR | /* Card controller error */ \
  1078. R1_ERROR) /* General/unknown error */
  1079. #define CMD_ERRORS \
  1080. (CMD_ERRORS_EXCL_OOR | \
  1081. R1_OUT_OF_RANGE) /* Command argument out of range */ \
  1082. static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
  1083. {
  1084. u32 val;
  1085. /*
  1086. * Per the SD specification(physical layer version 4.10)[1],
  1087. * section 4.3.3, it explicitly states that "When the last
  1088. * block of user area is read using CMD18, the host should
  1089. * ignore OUT_OF_RANGE error that may occur even the sequence
  1090. * is correct". And JESD84-B51 for eMMC also has a similar
  1091. * statement on section 6.8.3.
  1092. *
  1093. * Multiple block read/write could be done by either predefined
  1094. * method, namely CMD23, or open-ending mode. For open-ending mode,
  1095. * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
  1096. *
  1097. * However the spec[1] doesn't tell us whether we should also
  1098. * ignore that for predefined method. But per the spec[1], section
  1099. * 4.15 Set Block Count Command, it says"If illegal block count
  1100. * is set, out of range error will be indicated during read/write
  1101. * operation (For example, data transfer is stopped at user area
  1102. * boundary)." In another word, we could expect a out of range error
  1103. * in the response for the following CMD18/25. And if argument of
  1104. * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
  1105. * we could also expect to get a -ETIMEDOUT or any error number from
  1106. * the host drivers due to missing data response(for write)/data(for
  1107. * read), as the cards will stop the data transfer by itself per the
  1108. * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
  1109. */
  1110. if (!brq->stop.error) {
  1111. bool oor_with_open_end;
  1112. /* If there is no error yet, check R1 response */
  1113. val = brq->stop.resp[0] & CMD_ERRORS;
  1114. oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
  1115. if (val && !oor_with_open_end)
  1116. brq->stop.error = -EIO;
  1117. }
  1118. }
  1119. static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
  1120. int disable_multi, bool *do_rel_wr_p,
  1121. bool *do_data_tag_p)
  1122. {
  1123. struct mmc_blk_data *md = mq->blkdata;
  1124. struct mmc_card *card = md->queue.card;
  1125. struct mmc_blk_request *brq = &mqrq->brq;
  1126. struct request *req = mmc_queue_req_to_req(mqrq);
  1127. bool do_rel_wr, do_data_tag;
  1128. /*
  1129. * Reliable writes are used to implement Forced Unit Access and
  1130. * are supported only on MMCs.
  1131. */
  1132. do_rel_wr = (req->cmd_flags & REQ_FUA) &&
  1133. rq_data_dir(req) == WRITE &&
  1134. (md->flags & MMC_BLK_REL_WR);
  1135. memset(brq, 0, sizeof(struct mmc_blk_request));
  1136. brq->mrq.data = &brq->data;
  1137. brq->mrq.tag = req->tag;
  1138. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1139. brq->stop.arg = 0;
  1140. if (rq_data_dir(req) == READ) {
  1141. brq->data.flags = MMC_DATA_READ;
  1142. brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1143. } else {
  1144. brq->data.flags = MMC_DATA_WRITE;
  1145. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1146. }
  1147. brq->data.blksz = 512;
  1148. brq->data.blocks = blk_rq_sectors(req);
  1149. brq->data.blk_addr = blk_rq_pos(req);
  1150. /*
  1151. * The command queue supports 2 priorities: "high" (1) and "simple" (0).
  1152. * The eMMC will give "high" priority tasks priority over "simple"
  1153. * priority tasks. Here we always set "simple" priority by not setting
  1154. * MMC_DATA_PRIO.
  1155. */
  1156. /*
  1157. * The block layer doesn't support all sector count
  1158. * restrictions, so we need to be prepared for too big
  1159. * requests.
  1160. */
  1161. if (brq->data.blocks > card->host->max_blk_count)
  1162. brq->data.blocks = card->host->max_blk_count;
  1163. if (brq->data.blocks > 1) {
  1164. /*
  1165. * After a read error, we redo the request one sector
  1166. * at a time in order to accurately determine which
  1167. * sectors can be read successfully.
  1168. */
  1169. if (disable_multi)
  1170. brq->data.blocks = 1;
  1171. /*
  1172. * Some controllers have HW issues while operating
  1173. * in multiple I/O mode
  1174. */
  1175. if (card->host->ops->multi_io_quirk)
  1176. brq->data.blocks = card->host->ops->multi_io_quirk(card,
  1177. (rq_data_dir(req) == READ) ?
  1178. MMC_DATA_READ : MMC_DATA_WRITE,
  1179. brq->data.blocks);
  1180. }
  1181. if (do_rel_wr) {
  1182. mmc_apply_rel_rw(brq, card, req);
  1183. brq->data.flags |= MMC_DATA_REL_WR;
  1184. }
  1185. /*
  1186. * Data tag is used only during writing meta data to speed
  1187. * up write and any subsequent read of this meta data
  1188. */
  1189. do_data_tag = card->ext_csd.data_tag_unit_size &&
  1190. (req->cmd_flags & REQ_META) &&
  1191. (rq_data_dir(req) == WRITE) &&
  1192. ((brq->data.blocks * brq->data.blksz) >=
  1193. card->ext_csd.data_tag_unit_size);
  1194. if (do_data_tag)
  1195. brq->data.flags |= MMC_DATA_DAT_TAG;
  1196. mmc_set_data_timeout(&brq->data, card);
  1197. brq->data.sg = mqrq->sg;
  1198. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1199. /*
  1200. * Adjust the sg list so it is the same size as the
  1201. * request.
  1202. */
  1203. if (brq->data.blocks != blk_rq_sectors(req)) {
  1204. int i, data_size = brq->data.blocks << 9;
  1205. struct scatterlist *sg;
  1206. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1207. data_size -= sg->length;
  1208. if (data_size <= 0) {
  1209. sg->length += data_size;
  1210. i++;
  1211. break;
  1212. }
  1213. }
  1214. brq->data.sg_len = i;
  1215. }
  1216. if (do_rel_wr_p)
  1217. *do_rel_wr_p = do_rel_wr;
  1218. if (do_data_tag_p)
  1219. *do_data_tag_p = do_data_tag;
  1220. }
  1221. #define MMC_CQE_RETRIES 2
  1222. static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
  1223. {
  1224. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1225. struct mmc_request *mrq = &mqrq->brq.mrq;
  1226. struct request_queue *q = req->q;
  1227. struct mmc_host *host = mq->card->host;
  1228. unsigned long flags;
  1229. bool put_card;
  1230. int err;
  1231. mmc_cqe_post_req(host, mrq);
  1232. if (mrq->cmd && mrq->cmd->error)
  1233. err = mrq->cmd->error;
  1234. else if (mrq->data && mrq->data->error)
  1235. err = mrq->data->error;
  1236. else
  1237. err = 0;
  1238. if (err) {
  1239. if (mqrq->retries++ < MMC_CQE_RETRIES)
  1240. blk_mq_requeue_request(req, true);
  1241. else
  1242. blk_mq_end_request(req, BLK_STS_IOERR);
  1243. } else if (mrq->data) {
  1244. if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
  1245. blk_mq_requeue_request(req, true);
  1246. else
  1247. __blk_mq_end_request(req, BLK_STS_OK);
  1248. } else {
  1249. blk_mq_end_request(req, BLK_STS_OK);
  1250. }
  1251. spin_lock_irqsave(q->queue_lock, flags);
  1252. mq->in_flight[mmc_issue_type(mq, req)] -= 1;
  1253. put_card = (mmc_tot_in_flight(mq) == 0);
  1254. mmc_cqe_check_busy(mq);
  1255. spin_unlock_irqrestore(q->queue_lock, flags);
  1256. if (!mq->cqe_busy)
  1257. blk_mq_run_hw_queues(q, true);
  1258. if (put_card)
  1259. mmc_put_card(mq->card, &mq->ctx);
  1260. }
  1261. void mmc_blk_cqe_recovery(struct mmc_queue *mq)
  1262. {
  1263. struct mmc_card *card = mq->card;
  1264. struct mmc_host *host = card->host;
  1265. int err;
  1266. pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
  1267. err = mmc_cqe_recovery(host);
  1268. if (err)
  1269. mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
  1270. else
  1271. mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
  1272. pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
  1273. }
  1274. static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
  1275. {
  1276. struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
  1277. brq.mrq);
  1278. struct request *req = mmc_queue_req_to_req(mqrq);
  1279. struct request_queue *q = req->q;
  1280. struct mmc_queue *mq = q->queuedata;
  1281. /*
  1282. * Block layer timeouts race with completions which means the normal
  1283. * completion path cannot be used during recovery.
  1284. */
  1285. if (mq->in_recovery)
  1286. mmc_blk_cqe_complete_rq(mq, req);
  1287. else
  1288. blk_mq_complete_request(req);
  1289. }
  1290. static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
  1291. {
  1292. mrq->done = mmc_blk_cqe_req_done;
  1293. mrq->recovery_notifier = mmc_cqe_recovery_notifier;
  1294. return mmc_cqe_start_req(host, mrq);
  1295. }
  1296. static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
  1297. struct request *req)
  1298. {
  1299. struct mmc_blk_request *brq = &mqrq->brq;
  1300. memset(brq, 0, sizeof(*brq));
  1301. brq->mrq.cmd = &brq->cmd;
  1302. brq->mrq.tag = req->tag;
  1303. return &brq->mrq;
  1304. }
  1305. static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
  1306. {
  1307. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1308. struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
  1309. mrq->cmd->opcode = MMC_SWITCH;
  1310. mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  1311. (EXT_CSD_FLUSH_CACHE << 16) |
  1312. (1 << 8) |
  1313. EXT_CSD_CMD_SET_NORMAL;
  1314. mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
  1315. return mmc_blk_cqe_start_req(mq->card->host, mrq);
  1316. }
  1317. static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  1318. {
  1319. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1320. mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
  1321. return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
  1322. }
  1323. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1324. struct mmc_card *card,
  1325. int disable_multi,
  1326. struct mmc_queue *mq)
  1327. {
  1328. u32 readcmd, writecmd;
  1329. struct mmc_blk_request *brq = &mqrq->brq;
  1330. struct request *req = mmc_queue_req_to_req(mqrq);
  1331. struct mmc_blk_data *md = mq->blkdata;
  1332. bool do_rel_wr, do_data_tag;
  1333. mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
  1334. brq->mrq.cmd = &brq->cmd;
  1335. brq->cmd.arg = blk_rq_pos(req);
  1336. if (!mmc_card_blockaddr(card))
  1337. brq->cmd.arg <<= 9;
  1338. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1339. if (brq->data.blocks > 1 || do_rel_wr) {
  1340. /* SPI multiblock writes terminate using a special
  1341. * token, not a STOP_TRANSMISSION request.
  1342. */
  1343. if (!mmc_host_is_spi(card->host) ||
  1344. rq_data_dir(req) == READ)
  1345. brq->mrq.stop = &brq->stop;
  1346. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1347. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1348. } else {
  1349. brq->mrq.stop = NULL;
  1350. readcmd = MMC_READ_SINGLE_BLOCK;
  1351. writecmd = MMC_WRITE_BLOCK;
  1352. }
  1353. brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
  1354. /*
  1355. * Pre-defined multi-block transfers are preferable to
  1356. * open ended-ones (and necessary for reliable writes).
  1357. * However, it is not sufficient to just send CMD23,
  1358. * and avoid the final CMD12, as on an error condition
  1359. * CMD12 (stop) needs to be sent anyway. This, coupled
  1360. * with Auto-CMD23 enhancements provided by some
  1361. * hosts, means that the complexity of dealing
  1362. * with this is best left to the host. If CMD23 is
  1363. * supported by card and host, we'll fill sbc in and let
  1364. * the host deal with handling it correctly. This means
  1365. * that for hosts that don't expose MMC_CAP_CMD23, no
  1366. * change of behavior will be observed.
  1367. *
  1368. * N.B: Some MMC cards experience perf degradation.
  1369. * We'll avoid using CMD23-bounded multiblock writes for
  1370. * these, while retaining features like reliable writes.
  1371. */
  1372. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1373. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1374. do_data_tag)) {
  1375. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1376. brq->sbc.arg = brq->data.blocks |
  1377. (do_rel_wr ? (1 << 31) : 0) |
  1378. (do_data_tag ? (1 << 29) : 0);
  1379. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1380. brq->mrq.sbc = &brq->sbc;
  1381. }
  1382. }
  1383. #define MMC_MAX_RETRIES 5
  1384. #define MMC_DATA_RETRIES 2
  1385. #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
  1386. static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
  1387. {
  1388. struct mmc_command cmd = {
  1389. .opcode = MMC_STOP_TRANSMISSION,
  1390. .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
  1391. /* Some hosts wait for busy anyway, so provide a busy timeout */
  1392. .busy_timeout = timeout,
  1393. };
  1394. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1395. }
  1396. static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
  1397. {
  1398. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1399. struct mmc_blk_request *brq = &mqrq->brq;
  1400. unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
  1401. int err;
  1402. mmc_retune_hold_now(card->host);
  1403. mmc_blk_send_stop(card, timeout);
  1404. err = card_busy_detect(card, timeout, req, NULL);
  1405. mmc_retune_release(card->host);
  1406. return err;
  1407. }
  1408. #define MMC_READ_SINGLE_RETRIES 2
  1409. /* Single sector read during recovery */
  1410. static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
  1411. {
  1412. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1413. struct mmc_request *mrq = &mqrq->brq.mrq;
  1414. struct mmc_card *card = mq->card;
  1415. struct mmc_host *host = card->host;
  1416. blk_status_t error = BLK_STS_OK;
  1417. int retries = 0;
  1418. do {
  1419. u32 status;
  1420. int err;
  1421. mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
  1422. mmc_wait_for_req(host, mrq);
  1423. err = mmc_send_status(card, &status);
  1424. if (err)
  1425. goto error_exit;
  1426. if (!mmc_host_is_spi(host) &&
  1427. !mmc_blk_in_tran_state(status)) {
  1428. err = mmc_blk_fix_state(card, req);
  1429. if (err)
  1430. goto error_exit;
  1431. }
  1432. if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
  1433. continue;
  1434. retries = 0;
  1435. if (mrq->cmd->error ||
  1436. mrq->data->error ||
  1437. (!mmc_host_is_spi(host) &&
  1438. (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
  1439. error = BLK_STS_IOERR;
  1440. else
  1441. error = BLK_STS_OK;
  1442. } while (blk_update_request(req, error, 512));
  1443. return;
  1444. error_exit:
  1445. mrq->data->bytes_xfered = 0;
  1446. blk_update_request(req, BLK_STS_IOERR, 512);
  1447. /* Let it try the remaining request again */
  1448. if (mqrq->retries > MMC_MAX_RETRIES - 1)
  1449. mqrq->retries = MMC_MAX_RETRIES - 1;
  1450. }
  1451. static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
  1452. {
  1453. return !!brq->mrq.sbc;
  1454. }
  1455. static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
  1456. {
  1457. return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
  1458. }
  1459. /*
  1460. * Check for errors the host controller driver might not have seen such as
  1461. * response mode errors or invalid card state.
  1462. */
  1463. static bool mmc_blk_status_error(struct request *req, u32 status)
  1464. {
  1465. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1466. struct mmc_blk_request *brq = &mqrq->brq;
  1467. struct mmc_queue *mq = req->q->queuedata;
  1468. u32 stop_err_bits;
  1469. if (mmc_host_is_spi(mq->card->host))
  1470. return false;
  1471. stop_err_bits = mmc_blk_stop_err_bits(brq);
  1472. return brq->cmd.resp[0] & CMD_ERRORS ||
  1473. brq->stop.resp[0] & stop_err_bits ||
  1474. status & stop_err_bits ||
  1475. (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
  1476. }
  1477. static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
  1478. {
  1479. return !brq->sbc.error && !brq->cmd.error &&
  1480. !(brq->cmd.resp[0] & CMD_ERRORS);
  1481. }
  1482. /*
  1483. * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
  1484. * policy:
  1485. * 1. A request that has transferred at least some data is considered
  1486. * successful and will be requeued if there is remaining data to
  1487. * transfer.
  1488. * 2. Otherwise the number of retries is incremented and the request
  1489. * will be requeued if there are remaining retries.
  1490. * 3. Otherwise the request will be errored out.
  1491. * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
  1492. * mqrq->retries. So there are only 4 possible actions here:
  1493. * 1. do not accept the bytes_xfered value i.e. set it to zero
  1494. * 2. change mqrq->retries to determine the number of retries
  1495. * 3. try to reset the card
  1496. * 4. read one sector at a time
  1497. */
  1498. static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
  1499. {
  1500. int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1501. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1502. struct mmc_blk_request *brq = &mqrq->brq;
  1503. struct mmc_blk_data *md = mq->blkdata;
  1504. struct mmc_card *card = mq->card;
  1505. u32 status;
  1506. u32 blocks;
  1507. int err;
  1508. /*
  1509. * Some errors the host driver might not have seen. Set the number of
  1510. * bytes transferred to zero in that case.
  1511. */
  1512. err = __mmc_send_status(card, &status, 0);
  1513. if (err || mmc_blk_status_error(req, status))
  1514. brq->data.bytes_xfered = 0;
  1515. mmc_retune_release(card->host);
  1516. /*
  1517. * Try again to get the status. This also provides an opportunity for
  1518. * re-tuning.
  1519. */
  1520. if (err)
  1521. err = __mmc_send_status(card, &status, 0);
  1522. /*
  1523. * Nothing more to do after the number of bytes transferred has been
  1524. * updated and there is no card.
  1525. */
  1526. if (err && mmc_detect_card_removed(card->host))
  1527. return;
  1528. /* Try to get back to "tran" state */
  1529. if (!mmc_host_is_spi(mq->card->host) &&
  1530. (err || !mmc_blk_in_tran_state(status)))
  1531. err = mmc_blk_fix_state(mq->card, req);
  1532. /*
  1533. * Special case for SD cards where the card might record the number of
  1534. * blocks written.
  1535. */
  1536. if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
  1537. rq_data_dir(req) == WRITE) {
  1538. if (mmc_sd_num_wr_blocks(card, &blocks))
  1539. brq->data.bytes_xfered = 0;
  1540. else
  1541. brq->data.bytes_xfered = blocks << 9;
  1542. }
  1543. /* Reset if the card is in a bad state */
  1544. if (!mmc_host_is_spi(mq->card->host) &&
  1545. err && mmc_blk_reset(md, card->host, type)) {
  1546. pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
  1547. mqrq->retries = MMC_NO_RETRIES;
  1548. return;
  1549. }
  1550. /*
  1551. * If anything was done, just return and if there is anything remaining
  1552. * on the request it will get requeued.
  1553. */
  1554. if (brq->data.bytes_xfered)
  1555. return;
  1556. /* Reset before last retry */
  1557. if (mqrq->retries + 1 == MMC_MAX_RETRIES)
  1558. mmc_blk_reset(md, card->host, type);
  1559. /* Command errors fail fast, so use all MMC_MAX_RETRIES */
  1560. if (brq->sbc.error || brq->cmd.error)
  1561. return;
  1562. /* Reduce the remaining retries for data errors */
  1563. if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
  1564. mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
  1565. return;
  1566. }
  1567. /* FIXME: Missing single sector read for large sector size */
  1568. if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
  1569. brq->data.blocks > 1) {
  1570. /* Read one sector at a time */
  1571. mmc_blk_read_single(mq, req);
  1572. return;
  1573. }
  1574. }
  1575. static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
  1576. {
  1577. mmc_blk_eval_resp_error(brq);
  1578. return brq->sbc.error || brq->cmd.error || brq->stop.error ||
  1579. brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
  1580. }
  1581. static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
  1582. {
  1583. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1584. u32 status = 0;
  1585. int err;
  1586. if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
  1587. return 0;
  1588. err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &status);
  1589. /*
  1590. * Do not assume data transferred correctly if there are any error bits
  1591. * set.
  1592. */
  1593. if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
  1594. mqrq->brq.data.bytes_xfered = 0;
  1595. err = err ? err : -EIO;
  1596. }
  1597. /* Copy the exception bit so it will be seen later on */
  1598. if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
  1599. mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
  1600. return err;
  1601. }
  1602. static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
  1603. struct request *req)
  1604. {
  1605. int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1606. mmc_blk_reset_success(mq->blkdata, type);
  1607. }
  1608. static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
  1609. {
  1610. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1611. unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
  1612. if (nr_bytes) {
  1613. if (blk_update_request(req, BLK_STS_OK, nr_bytes))
  1614. blk_mq_requeue_request(req, true);
  1615. else
  1616. __blk_mq_end_request(req, BLK_STS_OK);
  1617. } else if (!blk_rq_bytes(req)) {
  1618. __blk_mq_end_request(req, BLK_STS_IOERR);
  1619. } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
  1620. blk_mq_requeue_request(req, true);
  1621. } else {
  1622. if (mmc_card_removed(mq->card))
  1623. req->rq_flags |= RQF_QUIET;
  1624. blk_mq_end_request(req, BLK_STS_IOERR);
  1625. }
  1626. }
  1627. static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
  1628. struct mmc_queue_req *mqrq)
  1629. {
  1630. return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
  1631. (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
  1632. mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
  1633. }
  1634. static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
  1635. struct mmc_queue_req *mqrq)
  1636. {
  1637. if (mmc_blk_urgent_bkops_needed(mq, mqrq))
  1638. mmc_start_bkops(mq->card, true);
  1639. }
  1640. void mmc_blk_mq_complete(struct request *req)
  1641. {
  1642. struct mmc_queue *mq = req->q->queuedata;
  1643. if (mq->use_cqe)
  1644. mmc_blk_cqe_complete_rq(mq, req);
  1645. else
  1646. mmc_blk_mq_complete_rq(mq, req);
  1647. }
  1648. static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
  1649. struct request *req)
  1650. {
  1651. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1652. struct mmc_host *host = mq->card->host;
  1653. if (mmc_blk_rq_error(&mqrq->brq) ||
  1654. mmc_blk_card_busy(mq->card, req)) {
  1655. mmc_blk_mq_rw_recovery(mq, req);
  1656. } else {
  1657. mmc_blk_rw_reset_success(mq, req);
  1658. mmc_retune_release(host);
  1659. }
  1660. mmc_blk_urgent_bkops(mq, mqrq);
  1661. }
  1662. static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
  1663. {
  1664. struct request_queue *q = req->q;
  1665. unsigned long flags;
  1666. bool put_card;
  1667. spin_lock_irqsave(q->queue_lock, flags);
  1668. mq->in_flight[mmc_issue_type(mq, req)] -= 1;
  1669. put_card = (mmc_tot_in_flight(mq) == 0);
  1670. spin_unlock_irqrestore(q->queue_lock, flags);
  1671. if (put_card)
  1672. mmc_put_card(mq->card, &mq->ctx);
  1673. }
  1674. static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
  1675. {
  1676. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1677. struct mmc_request *mrq = &mqrq->brq.mrq;
  1678. struct mmc_host *host = mq->card->host;
  1679. mmc_post_req(host, mrq, 0);
  1680. /*
  1681. * Block layer timeouts race with completions which means the normal
  1682. * completion path cannot be used during recovery.
  1683. */
  1684. if (mq->in_recovery)
  1685. mmc_blk_mq_complete_rq(mq, req);
  1686. else
  1687. blk_mq_complete_request(req);
  1688. mmc_blk_mq_dec_in_flight(mq, req);
  1689. }
  1690. void mmc_blk_mq_recovery(struct mmc_queue *mq)
  1691. {
  1692. struct request *req = mq->recovery_req;
  1693. struct mmc_host *host = mq->card->host;
  1694. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1695. mq->recovery_req = NULL;
  1696. mq->rw_wait = false;
  1697. if (mmc_blk_rq_error(&mqrq->brq)) {
  1698. mmc_retune_hold_now(host);
  1699. mmc_blk_mq_rw_recovery(mq, req);
  1700. }
  1701. mmc_blk_urgent_bkops(mq, mqrq);
  1702. mmc_blk_mq_post_req(mq, req);
  1703. }
  1704. static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
  1705. struct request **prev_req)
  1706. {
  1707. if (mmc_host_done_complete(mq->card->host))
  1708. return;
  1709. mutex_lock(&mq->complete_lock);
  1710. if (!mq->complete_req)
  1711. goto out_unlock;
  1712. mmc_blk_mq_poll_completion(mq, mq->complete_req);
  1713. if (prev_req)
  1714. *prev_req = mq->complete_req;
  1715. else
  1716. mmc_blk_mq_post_req(mq, mq->complete_req);
  1717. mq->complete_req = NULL;
  1718. out_unlock:
  1719. mutex_unlock(&mq->complete_lock);
  1720. }
  1721. void mmc_blk_mq_complete_work(struct work_struct *work)
  1722. {
  1723. struct mmc_queue *mq = container_of(work, struct mmc_queue,
  1724. complete_work);
  1725. mmc_blk_mq_complete_prev_req(mq, NULL);
  1726. }
  1727. static void mmc_blk_mq_req_done(struct mmc_request *mrq)
  1728. {
  1729. struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
  1730. brq.mrq);
  1731. struct request *req = mmc_queue_req_to_req(mqrq);
  1732. struct request_queue *q = req->q;
  1733. struct mmc_queue *mq = q->queuedata;
  1734. struct mmc_host *host = mq->card->host;
  1735. unsigned long flags;
  1736. if (!mmc_host_done_complete(host)) {
  1737. bool waiting;
  1738. /*
  1739. * We cannot complete the request in this context, so record
  1740. * that there is a request to complete, and that a following
  1741. * request does not need to wait (although it does need to
  1742. * complete complete_req first).
  1743. */
  1744. spin_lock_irqsave(q->queue_lock, flags);
  1745. mq->complete_req = req;
  1746. mq->rw_wait = false;
  1747. waiting = mq->waiting;
  1748. spin_unlock_irqrestore(q->queue_lock, flags);
  1749. /*
  1750. * If 'waiting' then the waiting task will complete this
  1751. * request, otherwise queue a work to do it. Note that
  1752. * complete_work may still race with the dispatch of a following
  1753. * request.
  1754. */
  1755. if (waiting)
  1756. wake_up(&mq->wait);
  1757. else
  1758. kblockd_schedule_work(&mq->complete_work);
  1759. return;
  1760. }
  1761. /* Take the recovery path for errors or urgent background operations */
  1762. if (mmc_blk_rq_error(&mqrq->brq) ||
  1763. mmc_blk_urgent_bkops_needed(mq, mqrq)) {
  1764. spin_lock_irqsave(q->queue_lock, flags);
  1765. mq->recovery_needed = true;
  1766. mq->recovery_req = req;
  1767. spin_unlock_irqrestore(q->queue_lock, flags);
  1768. wake_up(&mq->wait);
  1769. schedule_work(&mq->recovery_work);
  1770. return;
  1771. }
  1772. mmc_blk_rw_reset_success(mq, req);
  1773. mq->rw_wait = false;
  1774. wake_up(&mq->wait);
  1775. mmc_blk_mq_post_req(mq, req);
  1776. }
  1777. static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
  1778. {
  1779. struct request_queue *q = mq->queue;
  1780. unsigned long flags;
  1781. bool done;
  1782. /*
  1783. * Wait while there is another request in progress, but not if recovery
  1784. * is needed. Also indicate whether there is a request waiting to start.
  1785. */
  1786. spin_lock_irqsave(q->queue_lock, flags);
  1787. if (mq->recovery_needed) {
  1788. *err = -EBUSY;
  1789. done = true;
  1790. } else {
  1791. done = !mq->rw_wait;
  1792. }
  1793. mq->waiting = !done;
  1794. spin_unlock_irqrestore(q->queue_lock, flags);
  1795. return done;
  1796. }
  1797. static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
  1798. {
  1799. int err = 0;
  1800. wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
  1801. /* Always complete the previous request if there is one */
  1802. mmc_blk_mq_complete_prev_req(mq, prev_req);
  1803. return err;
  1804. }
  1805. static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
  1806. struct request *req)
  1807. {
  1808. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  1809. struct mmc_host *host = mq->card->host;
  1810. struct request *prev_req = NULL;
  1811. int err = 0;
  1812. mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
  1813. mqrq->brq.mrq.done = mmc_blk_mq_req_done;
  1814. mmc_pre_req(host, &mqrq->brq.mrq);
  1815. err = mmc_blk_rw_wait(mq, &prev_req);
  1816. if (err)
  1817. goto out_post_req;
  1818. mq->rw_wait = true;
  1819. err = mmc_start_request(host, &mqrq->brq.mrq);
  1820. if (prev_req)
  1821. mmc_blk_mq_post_req(mq, prev_req);
  1822. if (err)
  1823. mq->rw_wait = false;
  1824. /* Release re-tuning here where there is no synchronization required */
  1825. if (err || mmc_host_done_complete(host))
  1826. mmc_retune_release(host);
  1827. out_post_req:
  1828. if (err)
  1829. mmc_post_req(host, &mqrq->brq.mrq, err);
  1830. return err;
  1831. }
  1832. static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
  1833. {
  1834. if (mq->use_cqe)
  1835. return host->cqe_ops->cqe_wait_for_idle(host);
  1836. return mmc_blk_rw_wait(mq, NULL);
  1837. }
  1838. enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
  1839. {
  1840. struct mmc_blk_data *md = mq->blkdata;
  1841. struct mmc_card *card = md->queue.card;
  1842. struct mmc_host *host = card->host;
  1843. int ret;
  1844. ret = mmc_blk_part_switch(card, md->part_type);
  1845. if (ret)
  1846. return MMC_REQ_FAILED_TO_START;
  1847. switch (mmc_issue_type(mq, req)) {
  1848. case MMC_ISSUE_SYNC:
  1849. ret = mmc_blk_wait_for_idle(mq, host);
  1850. if (ret)
  1851. return MMC_REQ_BUSY;
  1852. switch (req_op(req)) {
  1853. case REQ_OP_DRV_IN:
  1854. case REQ_OP_DRV_OUT:
  1855. mmc_blk_issue_drv_op(mq, req);
  1856. break;
  1857. case REQ_OP_DISCARD:
  1858. mmc_blk_issue_discard_rq(mq, req);
  1859. break;
  1860. case REQ_OP_SECURE_ERASE:
  1861. mmc_blk_issue_secdiscard_rq(mq, req);
  1862. break;
  1863. case REQ_OP_FLUSH:
  1864. mmc_blk_issue_flush(mq, req);
  1865. break;
  1866. default:
  1867. WARN_ON_ONCE(1);
  1868. return MMC_REQ_FAILED_TO_START;
  1869. }
  1870. return MMC_REQ_FINISHED;
  1871. case MMC_ISSUE_DCMD:
  1872. case MMC_ISSUE_ASYNC:
  1873. switch (req_op(req)) {
  1874. case REQ_OP_FLUSH:
  1875. ret = mmc_blk_cqe_issue_flush(mq, req);
  1876. break;
  1877. case REQ_OP_READ:
  1878. case REQ_OP_WRITE:
  1879. if (mq->use_cqe)
  1880. ret = mmc_blk_cqe_issue_rw_rq(mq, req);
  1881. else
  1882. ret = mmc_blk_mq_issue_rw_rq(mq, req);
  1883. break;
  1884. default:
  1885. WARN_ON_ONCE(1);
  1886. ret = -EINVAL;
  1887. }
  1888. if (!ret)
  1889. return MMC_REQ_STARTED;
  1890. return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
  1891. default:
  1892. WARN_ON_ONCE(1);
  1893. return MMC_REQ_FAILED_TO_START;
  1894. }
  1895. }
  1896. static inline int mmc_blk_readonly(struct mmc_card *card)
  1897. {
  1898. return mmc_card_readonly(card) ||
  1899. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1900. }
  1901. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1902. struct device *parent,
  1903. sector_t size,
  1904. bool default_ro,
  1905. const char *subname,
  1906. int area_type)
  1907. {
  1908. struct mmc_blk_data *md;
  1909. int devidx, ret;
  1910. devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
  1911. if (devidx < 0) {
  1912. /*
  1913. * We get -ENOSPC because there are no more any available
  1914. * devidx. The reason may be that, either userspace haven't yet
  1915. * unmounted the partitions, which postpones mmc_blk_release()
  1916. * from being called, or the device has more partitions than
  1917. * what we support.
  1918. */
  1919. if (devidx == -ENOSPC)
  1920. dev_err(mmc_dev(card->host),
  1921. "no more device IDs available\n");
  1922. return ERR_PTR(devidx);
  1923. }
  1924. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1925. if (!md) {
  1926. ret = -ENOMEM;
  1927. goto out;
  1928. }
  1929. md->area_type = area_type;
  1930. /*
  1931. * Set the read-only status based on the supported commands
  1932. * and the write protect switch.
  1933. */
  1934. md->read_only = mmc_blk_readonly(card);
  1935. md->disk = alloc_disk(perdev_minors);
  1936. if (md->disk == NULL) {
  1937. ret = -ENOMEM;
  1938. goto err_kfree;
  1939. }
  1940. spin_lock_init(&md->lock);
  1941. INIT_LIST_HEAD(&md->part);
  1942. INIT_LIST_HEAD(&md->rpmbs);
  1943. md->usage = 1;
  1944. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1945. if (ret)
  1946. goto err_putdisk;
  1947. md->queue.blkdata = md;
  1948. /*
  1949. * Keep an extra reference to the queue so that we can shutdown the
  1950. * queue (i.e. call blk_cleanup_queue()) while there are still
  1951. * references to the 'md'. The corresponding blk_put_queue() is in
  1952. * mmc_blk_put().
  1953. */
  1954. if (!blk_get_queue(md->queue.queue)) {
  1955. mmc_cleanup_queue(&md->queue);
  1956. ret = -ENODEV;
  1957. goto err_putdisk;
  1958. }
  1959. md->disk->major = MMC_BLOCK_MAJOR;
  1960. md->disk->first_minor = devidx * perdev_minors;
  1961. md->disk->fops = &mmc_bdops;
  1962. md->disk->private_data = md;
  1963. md->disk->queue = md->queue.queue;
  1964. md->parent = parent;
  1965. set_disk_ro(md->disk, md->read_only || default_ro);
  1966. md->disk->flags = GENHD_FL_EXT_DEVT;
  1967. if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
  1968. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  1969. /*
  1970. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1971. *
  1972. * - be set for removable media with permanent block devices
  1973. * - be unset for removable block devices with permanent media
  1974. *
  1975. * Since MMC block devices clearly fall under the second
  1976. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1977. * should use the block device creation/destruction hotplug
  1978. * messages to tell when the card is present.
  1979. */
  1980. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1981. "mmcblk%u%s", card->host->index, subname ? subname : "");
  1982. if (mmc_card_mmc(card))
  1983. blk_queue_logical_block_size(md->queue.queue,
  1984. card->ext_csd.data_sector_size);
  1985. else
  1986. blk_queue_logical_block_size(md->queue.queue, 512);
  1987. set_capacity(md->disk, size);
  1988. if (mmc_host_cmd23(card->host)) {
  1989. if ((mmc_card_mmc(card) &&
  1990. card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
  1991. (mmc_card_sd(card) &&
  1992. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1993. md->flags |= MMC_BLK_CMD23;
  1994. }
  1995. if (mmc_card_mmc(card) &&
  1996. md->flags & MMC_BLK_CMD23 &&
  1997. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1998. card->ext_csd.rel_sectors)) {
  1999. md->flags |= MMC_BLK_REL_WR;
  2000. blk_queue_write_cache(md->queue.queue, true, true);
  2001. }
  2002. return md;
  2003. err_putdisk:
  2004. put_disk(md->disk);
  2005. err_kfree:
  2006. kfree(md);
  2007. out:
  2008. ida_simple_remove(&mmc_blk_ida, devidx);
  2009. return ERR_PTR(ret);
  2010. }
  2011. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  2012. {
  2013. sector_t size;
  2014. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  2015. /*
  2016. * The EXT_CSD sector count is in number or 512 byte
  2017. * sectors.
  2018. */
  2019. size = card->ext_csd.sectors;
  2020. } else {
  2021. /*
  2022. * The CSD capacity field is in units of read_blkbits.
  2023. * set_capacity takes units of 512 bytes.
  2024. */
  2025. size = (typeof(sector_t))card->csd.capacity
  2026. << (card->csd.read_blkbits - 9);
  2027. }
  2028. return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  2029. MMC_BLK_DATA_AREA_MAIN);
  2030. }
  2031. static int mmc_blk_alloc_part(struct mmc_card *card,
  2032. struct mmc_blk_data *md,
  2033. unsigned int part_type,
  2034. sector_t size,
  2035. bool default_ro,
  2036. const char *subname,
  2037. int area_type)
  2038. {
  2039. char cap_str[10];
  2040. struct mmc_blk_data *part_md;
  2041. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  2042. subname, area_type);
  2043. if (IS_ERR(part_md))
  2044. return PTR_ERR(part_md);
  2045. part_md->part_type = part_type;
  2046. list_add(&part_md->part, &md->part);
  2047. string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
  2048. cap_str, sizeof(cap_str));
  2049. pr_info("%s: %s %s partition %u %s\n",
  2050. part_md->disk->disk_name, mmc_card_id(card),
  2051. mmc_card_name(card), part_md->part_type, cap_str);
  2052. return 0;
  2053. }
  2054. /**
  2055. * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
  2056. * @filp: the character device file
  2057. * @cmd: the ioctl() command
  2058. * @arg: the argument from userspace
  2059. *
  2060. * This will essentially just redirect the ioctl()s coming in over to
  2061. * the main block device spawning the RPMB character device.
  2062. */
  2063. static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
  2064. unsigned long arg)
  2065. {
  2066. struct mmc_rpmb_data *rpmb = filp->private_data;
  2067. int ret;
  2068. switch (cmd) {
  2069. case MMC_IOC_CMD:
  2070. ret = mmc_blk_ioctl_cmd(rpmb->md,
  2071. (struct mmc_ioc_cmd __user *)arg,
  2072. rpmb);
  2073. break;
  2074. case MMC_IOC_MULTI_CMD:
  2075. ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
  2076. (struct mmc_ioc_multi_cmd __user *)arg,
  2077. rpmb);
  2078. break;
  2079. default:
  2080. ret = -EINVAL;
  2081. break;
  2082. }
  2083. return 0;
  2084. }
  2085. #ifdef CONFIG_COMPAT
  2086. static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
  2087. unsigned long arg)
  2088. {
  2089. return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  2090. }
  2091. #endif
  2092. static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
  2093. {
  2094. struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
  2095. struct mmc_rpmb_data, chrdev);
  2096. get_device(&rpmb->dev);
  2097. filp->private_data = rpmb;
  2098. mmc_blk_get(rpmb->md->disk);
  2099. return nonseekable_open(inode, filp);
  2100. }
  2101. static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
  2102. {
  2103. struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
  2104. struct mmc_rpmb_data, chrdev);
  2105. put_device(&rpmb->dev);
  2106. mmc_blk_put(rpmb->md);
  2107. return 0;
  2108. }
  2109. static const struct file_operations mmc_rpmb_fileops = {
  2110. .release = mmc_rpmb_chrdev_release,
  2111. .open = mmc_rpmb_chrdev_open,
  2112. .owner = THIS_MODULE,
  2113. .llseek = no_llseek,
  2114. .unlocked_ioctl = mmc_rpmb_ioctl,
  2115. #ifdef CONFIG_COMPAT
  2116. .compat_ioctl = mmc_rpmb_ioctl_compat,
  2117. #endif
  2118. };
  2119. static void mmc_blk_rpmb_device_release(struct device *dev)
  2120. {
  2121. struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
  2122. ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
  2123. kfree(rpmb);
  2124. }
  2125. static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
  2126. struct mmc_blk_data *md,
  2127. unsigned int part_index,
  2128. sector_t size,
  2129. const char *subname)
  2130. {
  2131. int devidx, ret;
  2132. char rpmb_name[DISK_NAME_LEN];
  2133. char cap_str[10];
  2134. struct mmc_rpmb_data *rpmb;
  2135. /* This creates the minor number for the RPMB char device */
  2136. devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
  2137. if (devidx < 0)
  2138. return devidx;
  2139. rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
  2140. if (!rpmb) {
  2141. ida_simple_remove(&mmc_rpmb_ida, devidx);
  2142. return -ENOMEM;
  2143. }
  2144. snprintf(rpmb_name, sizeof(rpmb_name),
  2145. "mmcblk%u%s", card->host->index, subname ? subname : "");
  2146. rpmb->id = devidx;
  2147. rpmb->part_index = part_index;
  2148. rpmb->dev.init_name = rpmb_name;
  2149. rpmb->dev.bus = &mmc_rpmb_bus_type;
  2150. rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
  2151. rpmb->dev.parent = &card->dev;
  2152. rpmb->dev.release = mmc_blk_rpmb_device_release;
  2153. device_initialize(&rpmb->dev);
  2154. dev_set_drvdata(&rpmb->dev, rpmb);
  2155. rpmb->md = md;
  2156. cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
  2157. rpmb->chrdev.owner = THIS_MODULE;
  2158. ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
  2159. if (ret) {
  2160. pr_err("%s: could not add character device\n", rpmb_name);
  2161. goto out_put_device;
  2162. }
  2163. list_add(&rpmb->node, &md->rpmbs);
  2164. string_get_size((u64)size, 512, STRING_UNITS_2,
  2165. cap_str, sizeof(cap_str));
  2166. pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
  2167. rpmb_name, mmc_card_id(card),
  2168. mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
  2169. MAJOR(mmc_rpmb_devt), rpmb->id);
  2170. return 0;
  2171. out_put_device:
  2172. put_device(&rpmb->dev);
  2173. return ret;
  2174. }
  2175. static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
  2176. {
  2177. cdev_device_del(&rpmb->chrdev, &rpmb->dev);
  2178. put_device(&rpmb->dev);
  2179. }
  2180. /* MMC Physical partitions consist of two boot partitions and
  2181. * up to four general purpose partitions.
  2182. * For each partition enabled in EXT_CSD a block device will be allocatedi
  2183. * to provide access to the partition.
  2184. */
  2185. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  2186. {
  2187. int idx, ret;
  2188. if (!mmc_card_mmc(card))
  2189. return 0;
  2190. for (idx = 0; idx < card->nr_parts; idx++) {
  2191. if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
  2192. /*
  2193. * RPMB partitions does not provide block access, they
  2194. * are only accessed using ioctl():s. Thus create
  2195. * special RPMB block devices that do not have a
  2196. * backing block queue for these.
  2197. */
  2198. ret = mmc_blk_alloc_rpmb_part(card, md,
  2199. card->part[idx].part_cfg,
  2200. card->part[idx].size >> 9,
  2201. card->part[idx].name);
  2202. if (ret)
  2203. return ret;
  2204. } else if (card->part[idx].size) {
  2205. ret = mmc_blk_alloc_part(card, md,
  2206. card->part[idx].part_cfg,
  2207. card->part[idx].size >> 9,
  2208. card->part[idx].force_ro,
  2209. card->part[idx].name,
  2210. card->part[idx].area_type);
  2211. if (ret)
  2212. return ret;
  2213. }
  2214. }
  2215. return 0;
  2216. }
  2217. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  2218. {
  2219. struct mmc_card *card;
  2220. if (md) {
  2221. /*
  2222. * Flush remaining requests and free queues. It
  2223. * is freeing the queue that stops new requests
  2224. * from being accepted.
  2225. */
  2226. card = md->queue.card;
  2227. if (md->disk->flags & GENHD_FL_UP) {
  2228. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2229. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  2230. card->ext_csd.boot_ro_lockable)
  2231. device_remove_file(disk_to_dev(md->disk),
  2232. &md->power_ro_lock);
  2233. del_gendisk(md->disk);
  2234. }
  2235. mmc_cleanup_queue(&md->queue);
  2236. mmc_blk_put(md);
  2237. }
  2238. }
  2239. static void mmc_blk_remove_parts(struct mmc_card *card,
  2240. struct mmc_blk_data *md)
  2241. {
  2242. struct list_head *pos, *q;
  2243. struct mmc_blk_data *part_md;
  2244. struct mmc_rpmb_data *rpmb;
  2245. /* Remove RPMB partitions */
  2246. list_for_each_safe(pos, q, &md->rpmbs) {
  2247. rpmb = list_entry(pos, struct mmc_rpmb_data, node);
  2248. list_del(pos);
  2249. mmc_blk_remove_rpmb_part(rpmb);
  2250. }
  2251. /* Remove block partitions */
  2252. list_for_each_safe(pos, q, &md->part) {
  2253. part_md = list_entry(pos, struct mmc_blk_data, part);
  2254. list_del(pos);
  2255. mmc_blk_remove_req(part_md);
  2256. }
  2257. }
  2258. static int mmc_add_disk(struct mmc_blk_data *md)
  2259. {
  2260. int ret;
  2261. struct mmc_card *card = md->queue.card;
  2262. device_add_disk(md->parent, md->disk);
  2263. md->force_ro.show = force_ro_show;
  2264. md->force_ro.store = force_ro_store;
  2265. sysfs_attr_init(&md->force_ro.attr);
  2266. md->force_ro.attr.name = "force_ro";
  2267. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  2268. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  2269. if (ret)
  2270. goto force_ro_fail;
  2271. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  2272. card->ext_csd.boot_ro_lockable) {
  2273. umode_t mode;
  2274. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  2275. mode = S_IRUGO;
  2276. else
  2277. mode = S_IRUGO | S_IWUSR;
  2278. md->power_ro_lock.show = power_ro_lock_show;
  2279. md->power_ro_lock.store = power_ro_lock_store;
  2280. sysfs_attr_init(&md->power_ro_lock.attr);
  2281. md->power_ro_lock.attr.mode = mode;
  2282. md->power_ro_lock.attr.name =
  2283. "ro_lock_until_next_power_on";
  2284. ret = device_create_file(disk_to_dev(md->disk),
  2285. &md->power_ro_lock);
  2286. if (ret)
  2287. goto power_ro_lock_fail;
  2288. }
  2289. return ret;
  2290. power_ro_lock_fail:
  2291. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2292. force_ro_fail:
  2293. del_gendisk(md->disk);
  2294. return ret;
  2295. }
  2296. #ifdef CONFIG_DEBUG_FS
  2297. static int mmc_dbg_card_status_get(void *data, u64 *val)
  2298. {
  2299. struct mmc_card *card = data;
  2300. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2301. struct mmc_queue *mq = &md->queue;
  2302. struct request *req;
  2303. int ret;
  2304. /* Ask the block layer about the card status */
  2305. req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
  2306. if (IS_ERR(req))
  2307. return PTR_ERR(req);
  2308. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
  2309. blk_execute_rq(mq->queue, NULL, req, 0);
  2310. ret = req_to_mmc_queue_req(req)->drv_op_result;
  2311. if (ret >= 0) {
  2312. *val = ret;
  2313. ret = 0;
  2314. }
  2315. blk_put_request(req);
  2316. return ret;
  2317. }
  2318. DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
  2319. NULL, "%08llx\n");
  2320. /* That is two digits * 512 + 1 for newline */
  2321. #define EXT_CSD_STR_LEN 1025
  2322. static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
  2323. {
  2324. struct mmc_card *card = inode->i_private;
  2325. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2326. struct mmc_queue *mq = &md->queue;
  2327. struct request *req;
  2328. char *buf;
  2329. ssize_t n = 0;
  2330. u8 *ext_csd;
  2331. int err, i;
  2332. buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
  2333. if (!buf)
  2334. return -ENOMEM;
  2335. /* Ask the block layer for the EXT CSD */
  2336. req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
  2337. if (IS_ERR(req)) {
  2338. err = PTR_ERR(req);
  2339. goto out_free;
  2340. }
  2341. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
  2342. req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
  2343. blk_execute_rq(mq->queue, NULL, req, 0);
  2344. err = req_to_mmc_queue_req(req)->drv_op_result;
  2345. blk_put_request(req);
  2346. if (err) {
  2347. pr_err("FAILED %d\n", err);
  2348. goto out_free;
  2349. }
  2350. for (i = 0; i < 512; i++)
  2351. n += sprintf(buf + n, "%02x", ext_csd[i]);
  2352. n += sprintf(buf + n, "\n");
  2353. if (n != EXT_CSD_STR_LEN) {
  2354. err = -EINVAL;
  2355. kfree(ext_csd);
  2356. goto out_free;
  2357. }
  2358. filp->private_data = buf;
  2359. kfree(ext_csd);
  2360. return 0;
  2361. out_free:
  2362. kfree(buf);
  2363. return err;
  2364. }
  2365. static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
  2366. size_t cnt, loff_t *ppos)
  2367. {
  2368. char *buf = filp->private_data;
  2369. return simple_read_from_buffer(ubuf, cnt, ppos,
  2370. buf, EXT_CSD_STR_LEN);
  2371. }
  2372. static int mmc_ext_csd_release(struct inode *inode, struct file *file)
  2373. {
  2374. kfree(file->private_data);
  2375. return 0;
  2376. }
  2377. static const struct file_operations mmc_dbg_ext_csd_fops = {
  2378. .open = mmc_ext_csd_open,
  2379. .read = mmc_ext_csd_read,
  2380. .release = mmc_ext_csd_release,
  2381. .llseek = default_llseek,
  2382. };
  2383. static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
  2384. {
  2385. struct dentry *root;
  2386. if (!card->debugfs_root)
  2387. return 0;
  2388. root = card->debugfs_root;
  2389. if (mmc_card_mmc(card) || mmc_card_sd(card)) {
  2390. md->status_dentry =
  2391. debugfs_create_file("status", S_IRUSR, root, card,
  2392. &mmc_dbg_card_status_fops);
  2393. if (!md->status_dentry)
  2394. return -EIO;
  2395. }
  2396. if (mmc_card_mmc(card)) {
  2397. md->ext_csd_dentry =
  2398. debugfs_create_file("ext_csd", S_IRUSR, root, card,
  2399. &mmc_dbg_ext_csd_fops);
  2400. if (!md->ext_csd_dentry)
  2401. return -EIO;
  2402. }
  2403. return 0;
  2404. }
  2405. static void mmc_blk_remove_debugfs(struct mmc_card *card,
  2406. struct mmc_blk_data *md)
  2407. {
  2408. if (!card->debugfs_root)
  2409. return;
  2410. if (!IS_ERR_OR_NULL(md->status_dentry)) {
  2411. debugfs_remove(md->status_dentry);
  2412. md->status_dentry = NULL;
  2413. }
  2414. if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
  2415. debugfs_remove(md->ext_csd_dentry);
  2416. md->ext_csd_dentry = NULL;
  2417. }
  2418. }
  2419. #else
  2420. static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
  2421. {
  2422. return 0;
  2423. }
  2424. static void mmc_blk_remove_debugfs(struct mmc_card *card,
  2425. struct mmc_blk_data *md)
  2426. {
  2427. }
  2428. #endif /* CONFIG_DEBUG_FS */
  2429. static int mmc_blk_probe(struct mmc_card *card)
  2430. {
  2431. struct mmc_blk_data *md, *part_md;
  2432. char cap_str[10];
  2433. /*
  2434. * Check that the card supports the command class(es) we need.
  2435. */
  2436. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  2437. return -ENODEV;
  2438. mmc_fixup_device(card, mmc_blk_fixups);
  2439. md = mmc_blk_alloc(card);
  2440. if (IS_ERR(md))
  2441. return PTR_ERR(md);
  2442. string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
  2443. cap_str, sizeof(cap_str));
  2444. pr_info("%s: %s %s %s %s\n",
  2445. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  2446. cap_str, md->read_only ? "(ro)" : "");
  2447. if (mmc_blk_alloc_parts(card, md))
  2448. goto out;
  2449. dev_set_drvdata(&card->dev, md);
  2450. if (mmc_add_disk(md))
  2451. goto out;
  2452. list_for_each_entry(part_md, &md->part, part) {
  2453. if (mmc_add_disk(part_md))
  2454. goto out;
  2455. }
  2456. /* Add two debugfs entries */
  2457. mmc_blk_add_debugfs(card, md);
  2458. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  2459. pm_runtime_use_autosuspend(&card->dev);
  2460. /*
  2461. * Don't enable runtime PM for SD-combo cards here. Leave that
  2462. * decision to be taken during the SDIO init sequence instead.
  2463. */
  2464. if (card->type != MMC_TYPE_SD_COMBO) {
  2465. pm_runtime_set_active(&card->dev);
  2466. pm_runtime_enable(&card->dev);
  2467. }
  2468. return 0;
  2469. out:
  2470. mmc_blk_remove_parts(card, md);
  2471. mmc_blk_remove_req(md);
  2472. return 0;
  2473. }
  2474. static void mmc_blk_remove(struct mmc_card *card)
  2475. {
  2476. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2477. mmc_blk_remove_debugfs(card, md);
  2478. mmc_blk_remove_parts(card, md);
  2479. pm_runtime_get_sync(&card->dev);
  2480. mmc_claim_host(card->host);
  2481. mmc_blk_part_switch(card, md->part_type);
  2482. mmc_release_host(card->host);
  2483. if (card->type != MMC_TYPE_SD_COMBO)
  2484. pm_runtime_disable(&card->dev);
  2485. pm_runtime_put_noidle(&card->dev);
  2486. mmc_blk_remove_req(md);
  2487. dev_set_drvdata(&card->dev, NULL);
  2488. }
  2489. static int _mmc_blk_suspend(struct mmc_card *card)
  2490. {
  2491. struct mmc_blk_data *part_md;
  2492. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2493. if (md) {
  2494. mmc_queue_suspend(&md->queue);
  2495. list_for_each_entry(part_md, &md->part, part) {
  2496. mmc_queue_suspend(&part_md->queue);
  2497. }
  2498. }
  2499. return 0;
  2500. }
  2501. static void mmc_blk_shutdown(struct mmc_card *card)
  2502. {
  2503. _mmc_blk_suspend(card);
  2504. }
  2505. #ifdef CONFIG_PM_SLEEP
  2506. static int mmc_blk_suspend(struct device *dev)
  2507. {
  2508. struct mmc_card *card = mmc_dev_to_card(dev);
  2509. return _mmc_blk_suspend(card);
  2510. }
  2511. static int mmc_blk_resume(struct device *dev)
  2512. {
  2513. struct mmc_blk_data *part_md;
  2514. struct mmc_blk_data *md = dev_get_drvdata(dev);
  2515. if (md) {
  2516. /*
  2517. * Resume involves the card going into idle state,
  2518. * so current partition is always the main one.
  2519. */
  2520. md->part_curr = md->part_type;
  2521. mmc_queue_resume(&md->queue);
  2522. list_for_each_entry(part_md, &md->part, part) {
  2523. mmc_queue_resume(&part_md->queue);
  2524. }
  2525. }
  2526. return 0;
  2527. }
  2528. #endif
  2529. static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
  2530. static struct mmc_driver mmc_driver = {
  2531. .drv = {
  2532. .name = "mmcblk",
  2533. .pm = &mmc_blk_pm_ops,
  2534. },
  2535. .probe = mmc_blk_probe,
  2536. .remove = mmc_blk_remove,
  2537. .shutdown = mmc_blk_shutdown,
  2538. };
  2539. static int __init mmc_blk_init(void)
  2540. {
  2541. int res;
  2542. res = bus_register(&mmc_rpmb_bus_type);
  2543. if (res < 0) {
  2544. pr_err("mmcblk: could not register RPMB bus type\n");
  2545. return res;
  2546. }
  2547. res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
  2548. if (res < 0) {
  2549. pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
  2550. goto out_bus_unreg;
  2551. }
  2552. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2553. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2554. max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
  2555. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2556. if (res)
  2557. goto out_chrdev_unreg;
  2558. res = mmc_register_driver(&mmc_driver);
  2559. if (res)
  2560. goto out_blkdev_unreg;
  2561. return 0;
  2562. out_blkdev_unreg:
  2563. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2564. out_chrdev_unreg:
  2565. unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
  2566. out_bus_unreg:
  2567. bus_unregister(&mmc_rpmb_bus_type);
  2568. return res;
  2569. }
  2570. static void __exit mmc_blk_exit(void)
  2571. {
  2572. mmc_unregister_driver(&mmc_driver);
  2573. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2574. unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
  2575. bus_unregister(&mmc_rpmb_bus_type);
  2576. }
  2577. module_init(mmc_blk_init);
  2578. module_exit(mmc_blk_exit);
  2579. MODULE_LICENSE("GPL");
  2580. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");