block.c 94 KB

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