block.c 75 KB

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