block.c 86 KB

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