block.c 76 KB

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