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