block.c 86 KB

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