block.c 76 KB

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