block.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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/mutex.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/string_helpers.h>
  33. #include <linux/delay.h>
  34. #include <linux/capability.h>
  35. #include <linux/compat.h>
  36. #include <linux/mmc/ioctl.h>
  37. #include <linux/mmc/card.h>
  38. #include <linux/mmc/host.h>
  39. #include <linux/mmc/mmc.h>
  40. #include <linux/mmc/sd.h>
  41. #include <asm/uaccess.h>
  42. #include "queue.h"
  43. MODULE_ALIAS("mmc:block");
  44. #ifdef MODULE_PARAM_PREFIX
  45. #undef MODULE_PARAM_PREFIX
  46. #endif
  47. #define MODULE_PARAM_PREFIX "mmcblk."
  48. #define INAND_CMD38_ARG_EXT_CSD 113
  49. #define INAND_CMD38_ARG_ERASE 0x00
  50. #define INAND_CMD38_ARG_TRIM 0x01
  51. #define INAND_CMD38_ARG_SECERASE 0x80
  52. #define INAND_CMD38_ARG_SECTRIM1 0x81
  53. #define INAND_CMD38_ARG_SECTRIM2 0x88
  54. #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  55. #define MMC_SANITIZE_REQ_TIMEOUT 240000
  56. #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
  57. #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
  58. (req->cmd_flags & REQ_META)) && \
  59. (rq_data_dir(req) == WRITE))
  60. #define PACKED_CMD_VER 0x01
  61. #define PACKED_CMD_WR 0x02
  62. static DEFINE_MUTEX(block_mutex);
  63. /*
  64. * The defaults come from config options but can be overriden by module
  65. * or bootarg options.
  66. */
  67. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  68. /*
  69. * We've only got one major, so number of mmcblk devices is
  70. * limited to 256 / number of minors per device.
  71. */
  72. static int max_devices;
  73. /* 256 minors, so at most 256 separate devices */
  74. static DECLARE_BITMAP(dev_use, 256);
  75. static DECLARE_BITMAP(name_use, 256);
  76. /*
  77. * There is one mmc_blk_data per slot.
  78. */
  79. struct mmc_blk_data {
  80. spinlock_t lock;
  81. struct gendisk *disk;
  82. struct mmc_queue queue;
  83. struct list_head part;
  84. unsigned int flags;
  85. #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
  86. #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
  87. #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
  88. unsigned int usage;
  89. unsigned int read_only;
  90. unsigned int part_type;
  91. unsigned int name_idx;
  92. unsigned int reset_done;
  93. #define MMC_BLK_READ BIT(0)
  94. #define MMC_BLK_WRITE BIT(1)
  95. #define MMC_BLK_DISCARD BIT(2)
  96. #define MMC_BLK_SECDISCARD BIT(3)
  97. /*
  98. * Only set in main mmc_blk_data associated
  99. * with mmc_card with mmc_set_drvdata, and keeps
  100. * track of the current selected device partition.
  101. */
  102. unsigned int part_curr;
  103. struct device_attribute force_ro;
  104. struct device_attribute power_ro_lock;
  105. int area_type;
  106. };
  107. static DEFINE_MUTEX(open_lock);
  108. enum {
  109. MMC_PACKED_NR_IDX = -1,
  110. MMC_PACKED_NR_ZERO,
  111. MMC_PACKED_NR_SINGLE,
  112. };
  113. module_param(perdev_minors, int, 0444);
  114. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  115. static inline int mmc_blk_part_switch(struct mmc_card *card,
  116. struct mmc_blk_data *md);
  117. static int get_card_status(struct mmc_card *card, u32 *status, int retries);
  118. static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
  119. {
  120. struct mmc_packed *packed = mqrq->packed;
  121. BUG_ON(!packed);
  122. mqrq->cmd_type = MMC_PACKED_NONE;
  123. packed->nr_entries = MMC_PACKED_NR_ZERO;
  124. packed->idx_failure = MMC_PACKED_NR_IDX;
  125. packed->retries = 0;
  126. packed->blocks = 0;
  127. }
  128. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  129. {
  130. struct mmc_blk_data *md;
  131. mutex_lock(&open_lock);
  132. md = disk->private_data;
  133. if (md && md->usage == 0)
  134. md = NULL;
  135. if (md)
  136. md->usage++;
  137. mutex_unlock(&open_lock);
  138. return md;
  139. }
  140. static inline int mmc_get_devidx(struct gendisk *disk)
  141. {
  142. int devmaj = MAJOR(disk_devt(disk));
  143. int devidx = MINOR(disk_devt(disk)) / perdev_minors;
  144. if (!devmaj)
  145. devidx = disk->first_minor / perdev_minors;
  146. return devidx;
  147. }
  148. static void mmc_blk_put(struct mmc_blk_data *md)
  149. {
  150. mutex_lock(&open_lock);
  151. md->usage--;
  152. if (md->usage == 0) {
  153. int devidx = mmc_get_devidx(md->disk);
  154. blk_cleanup_queue(md->queue.queue);
  155. __clear_bit(devidx, dev_use);
  156. put_disk(md->disk);
  157. kfree(md);
  158. }
  159. mutex_unlock(&open_lock);
  160. }
  161. static ssize_t power_ro_lock_show(struct device *dev,
  162. struct device_attribute *attr, char *buf)
  163. {
  164. int ret;
  165. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  166. struct mmc_card *card = md->queue.card;
  167. int locked = 0;
  168. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
  169. locked = 2;
  170. else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
  171. locked = 1;
  172. ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
  173. return ret;
  174. }
  175. static ssize_t power_ro_lock_store(struct device *dev,
  176. struct device_attribute *attr, const char *buf, size_t count)
  177. {
  178. int ret;
  179. struct mmc_blk_data *md, *part_md;
  180. struct mmc_card *card;
  181. unsigned long set;
  182. if (kstrtoul(buf, 0, &set))
  183. return -EINVAL;
  184. if (set != 1)
  185. return count;
  186. md = mmc_blk_get(dev_to_disk(dev));
  187. card = md->queue.card;
  188. mmc_claim_host(card->host);
  189. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
  190. card->ext_csd.boot_ro_lock |
  191. EXT_CSD_BOOT_WP_B_PWR_WP_EN,
  192. card->ext_csd.part_time);
  193. if (ret)
  194. pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
  195. else
  196. card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
  197. mmc_release_host(card->host);
  198. if (!ret) {
  199. pr_info("%s: Locking boot partition ro until next power on\n",
  200. md->disk->disk_name);
  201. set_disk_ro(md->disk, 1);
  202. list_for_each_entry(part_md, &md->part, part)
  203. if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
  204. pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
  205. set_disk_ro(part_md->disk, 1);
  206. }
  207. }
  208. mmc_blk_put(md);
  209. return count;
  210. }
  211. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  212. char *buf)
  213. {
  214. int ret;
  215. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  216. ret = snprintf(buf, PAGE_SIZE, "%d",
  217. get_disk_ro(dev_to_disk(dev)) ^
  218. md->read_only);
  219. mmc_blk_put(md);
  220. return ret;
  221. }
  222. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  223. const char *buf, size_t count)
  224. {
  225. int ret;
  226. char *end;
  227. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  228. unsigned long set = simple_strtoul(buf, &end, 0);
  229. if (end == buf) {
  230. ret = -EINVAL;
  231. goto out;
  232. }
  233. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  234. ret = count;
  235. out:
  236. mmc_blk_put(md);
  237. return ret;
  238. }
  239. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  240. {
  241. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  242. int ret = -ENXIO;
  243. mutex_lock(&block_mutex);
  244. if (md) {
  245. if (md->usage == 2)
  246. check_disk_change(bdev);
  247. ret = 0;
  248. if ((mode & FMODE_WRITE) && md->read_only) {
  249. mmc_blk_put(md);
  250. ret = -EROFS;
  251. }
  252. }
  253. mutex_unlock(&block_mutex);
  254. return ret;
  255. }
  256. static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
  257. {
  258. struct mmc_blk_data *md = disk->private_data;
  259. mutex_lock(&block_mutex);
  260. mmc_blk_put(md);
  261. mutex_unlock(&block_mutex);
  262. }
  263. static int
  264. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  265. {
  266. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  267. geo->heads = 4;
  268. geo->sectors = 16;
  269. return 0;
  270. }
  271. struct mmc_blk_ioc_data {
  272. struct mmc_ioc_cmd ic;
  273. unsigned char *buf;
  274. u64 buf_bytes;
  275. };
  276. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  277. struct mmc_ioc_cmd __user *user)
  278. {
  279. struct mmc_blk_ioc_data *idata;
  280. int err;
  281. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  282. if (!idata) {
  283. err = -ENOMEM;
  284. goto out;
  285. }
  286. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  287. err = -EFAULT;
  288. goto idata_err;
  289. }
  290. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  291. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  292. err = -EOVERFLOW;
  293. goto idata_err;
  294. }
  295. if (!idata->buf_bytes)
  296. return idata;
  297. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  298. if (!idata->buf) {
  299. err = -ENOMEM;
  300. goto idata_err;
  301. }
  302. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  303. idata->ic.data_ptr, idata->buf_bytes)) {
  304. err = -EFAULT;
  305. goto copy_err;
  306. }
  307. return idata;
  308. copy_err:
  309. kfree(idata->buf);
  310. idata_err:
  311. kfree(idata);
  312. out:
  313. return ERR_PTR(err);
  314. }
  315. static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
  316. u32 retries_max)
  317. {
  318. int err;
  319. u32 retry_count = 0;
  320. if (!status || !retries_max)
  321. return -EINVAL;
  322. do {
  323. err = get_card_status(card, status, 5);
  324. if (err)
  325. break;
  326. if (!R1_STATUS(*status) &&
  327. (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
  328. break; /* RPMB programming operation complete */
  329. /*
  330. * Rechedule to give the MMC device a chance to continue
  331. * processing the previous command without being polled too
  332. * frequently.
  333. */
  334. usleep_range(1000, 5000);
  335. } while (++retry_count < retries_max);
  336. if (retry_count == retries_max)
  337. err = -EPERM;
  338. return err;
  339. }
  340. static int ioctl_do_sanitize(struct mmc_card *card)
  341. {
  342. int err;
  343. if (!(mmc_can_sanitize(card) &&
  344. (card->host->caps2 & MMC_CAP2_SANITIZE))) {
  345. pr_warn("%s: %s - SANITIZE is not supported\n",
  346. mmc_hostname(card->host), __func__);
  347. err = -EOPNOTSUPP;
  348. goto out;
  349. }
  350. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  351. mmc_hostname(card->host), __func__);
  352. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  353. EXT_CSD_SANITIZE_START, 1,
  354. MMC_SANITIZE_REQ_TIMEOUT);
  355. if (err)
  356. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  357. mmc_hostname(card->host), __func__, err);
  358. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  359. __func__);
  360. out:
  361. return err;
  362. }
  363. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  364. struct mmc_ioc_cmd __user *ic_ptr)
  365. {
  366. struct mmc_blk_ioc_data *idata;
  367. struct mmc_blk_data *md;
  368. struct mmc_card *card;
  369. struct mmc_command cmd = {0};
  370. struct mmc_data data = {0};
  371. struct mmc_request mrq = {NULL};
  372. struct scatterlist sg;
  373. int err;
  374. int is_rpmb = false;
  375. u32 status = 0;
  376. /*
  377. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  378. * whole block device, not on a partition. This prevents overspray
  379. * between sibling partitions.
  380. */
  381. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  382. return -EPERM;
  383. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  384. if (IS_ERR(idata))
  385. return PTR_ERR(idata);
  386. md = mmc_blk_get(bdev->bd_disk);
  387. if (!md) {
  388. err = -EINVAL;
  389. goto cmd_err;
  390. }
  391. if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
  392. is_rpmb = true;
  393. card = md->queue.card;
  394. if (IS_ERR(card)) {
  395. err = PTR_ERR(card);
  396. goto cmd_done;
  397. }
  398. cmd.opcode = idata->ic.opcode;
  399. cmd.arg = idata->ic.arg;
  400. cmd.flags = idata->ic.flags;
  401. if (idata->buf_bytes) {
  402. data.sg = &sg;
  403. data.sg_len = 1;
  404. data.blksz = idata->ic.blksz;
  405. data.blocks = idata->ic.blocks;
  406. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  407. if (idata->ic.write_flag)
  408. data.flags = MMC_DATA_WRITE;
  409. else
  410. data.flags = MMC_DATA_READ;
  411. /* data.flags must already be set before doing this. */
  412. mmc_set_data_timeout(&data, card);
  413. /* Allow overriding the timeout_ns for empirical tuning. */
  414. if (idata->ic.data_timeout_ns)
  415. data.timeout_ns = idata->ic.data_timeout_ns;
  416. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  417. /*
  418. * Pretend this is a data transfer and rely on the
  419. * host driver to compute timeout. When all host
  420. * drivers support cmd.cmd_timeout for R1B, this
  421. * can be changed to:
  422. *
  423. * mrq.data = NULL;
  424. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  425. */
  426. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  427. }
  428. mrq.data = &data;
  429. }
  430. mrq.cmd = &cmd;
  431. mmc_claim_host(card->host);
  432. err = mmc_blk_part_switch(card, md);
  433. if (err)
  434. goto cmd_rel_host;
  435. if (idata->ic.is_acmd) {
  436. err = mmc_app_cmd(card->host, card);
  437. if (err)
  438. goto cmd_rel_host;
  439. }
  440. if (is_rpmb) {
  441. err = mmc_set_blockcount(card, data.blocks,
  442. idata->ic.write_flag & (1 << 31));
  443. if (err)
  444. goto cmd_rel_host;
  445. }
  446. if (MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) {
  447. err = ioctl_do_sanitize(card);
  448. if (err)
  449. pr_err("%s: ioctl_do_sanitize() failed. err = %d",
  450. __func__, err);
  451. goto cmd_rel_host;
  452. }
  453. mmc_wait_for_req(card->host, &mrq);
  454. if (cmd.error) {
  455. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  456. __func__, cmd.error);
  457. err = cmd.error;
  458. goto cmd_rel_host;
  459. }
  460. if (data.error) {
  461. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  462. __func__, data.error);
  463. err = data.error;
  464. goto cmd_rel_host;
  465. }
  466. /*
  467. * According to the SD specs, some commands require a delay after
  468. * issuing the command.
  469. */
  470. if (idata->ic.postsleep_min_us)
  471. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  472. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  473. err = -EFAULT;
  474. goto cmd_rel_host;
  475. }
  476. if (!idata->ic.write_flag) {
  477. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  478. idata->buf, idata->buf_bytes)) {
  479. err = -EFAULT;
  480. goto cmd_rel_host;
  481. }
  482. }
  483. if (is_rpmb) {
  484. /*
  485. * Ensure RPMB command has completed by polling CMD13
  486. * "Send Status".
  487. */
  488. err = ioctl_rpmb_card_status_poll(card, &status, 5);
  489. if (err)
  490. dev_err(mmc_dev(card->host),
  491. "%s: Card Status=0x%08X, error %d\n",
  492. __func__, status, err);
  493. }
  494. cmd_rel_host:
  495. mmc_release_host(card->host);
  496. cmd_done:
  497. mmc_blk_put(md);
  498. cmd_err:
  499. kfree(idata->buf);
  500. kfree(idata);
  501. return err;
  502. }
  503. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  504. unsigned int cmd, unsigned long arg)
  505. {
  506. int ret = -EINVAL;
  507. if (cmd == MMC_IOC_CMD)
  508. ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
  509. return ret;
  510. }
  511. #ifdef CONFIG_COMPAT
  512. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  513. unsigned int cmd, unsigned long arg)
  514. {
  515. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  516. }
  517. #endif
  518. static const struct block_device_operations mmc_bdops = {
  519. .open = mmc_blk_open,
  520. .release = mmc_blk_release,
  521. .getgeo = mmc_blk_getgeo,
  522. .owner = THIS_MODULE,
  523. .ioctl = mmc_blk_ioctl,
  524. #ifdef CONFIG_COMPAT
  525. .compat_ioctl = mmc_blk_compat_ioctl,
  526. #endif
  527. };
  528. static inline int mmc_blk_part_switch(struct mmc_card *card,
  529. struct mmc_blk_data *md)
  530. {
  531. int ret;
  532. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  533. if (main_md->part_curr == md->part_type)
  534. return 0;
  535. if (mmc_card_mmc(card)) {
  536. u8 part_config = card->ext_csd.part_config;
  537. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  538. part_config |= md->part_type;
  539. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  540. EXT_CSD_PART_CONFIG, part_config,
  541. card->ext_csd.part_time);
  542. if (ret)
  543. return ret;
  544. card->ext_csd.part_config = part_config;
  545. }
  546. main_md->part_curr = md->part_type;
  547. return 0;
  548. }
  549. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  550. {
  551. int err;
  552. u32 result;
  553. __be32 *blocks;
  554. struct mmc_request mrq = {NULL};
  555. struct mmc_command cmd = {0};
  556. struct mmc_data data = {0};
  557. struct scatterlist sg;
  558. cmd.opcode = MMC_APP_CMD;
  559. cmd.arg = card->rca << 16;
  560. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  561. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  562. if (err)
  563. return (u32)-1;
  564. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  565. return (u32)-1;
  566. memset(&cmd, 0, sizeof(struct mmc_command));
  567. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  568. cmd.arg = 0;
  569. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  570. data.blksz = 4;
  571. data.blocks = 1;
  572. data.flags = MMC_DATA_READ;
  573. data.sg = &sg;
  574. data.sg_len = 1;
  575. mmc_set_data_timeout(&data, card);
  576. mrq.cmd = &cmd;
  577. mrq.data = &data;
  578. blocks = kmalloc(4, GFP_KERNEL);
  579. if (!blocks)
  580. return (u32)-1;
  581. sg_init_one(&sg, blocks, 4);
  582. mmc_wait_for_req(card->host, &mrq);
  583. result = ntohl(*blocks);
  584. kfree(blocks);
  585. if (cmd.error || data.error)
  586. result = (u32)-1;
  587. return result;
  588. }
  589. static int send_stop(struct mmc_card *card, u32 *status)
  590. {
  591. struct mmc_command cmd = {0};
  592. int err;
  593. cmd.opcode = MMC_STOP_TRANSMISSION;
  594. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  595. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  596. if (err == 0)
  597. *status = cmd.resp[0];
  598. return err;
  599. }
  600. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  601. {
  602. struct mmc_command cmd = {0};
  603. int err;
  604. cmd.opcode = MMC_SEND_STATUS;
  605. if (!mmc_host_is_spi(card->host))
  606. cmd.arg = card->rca << 16;
  607. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  608. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  609. if (err == 0)
  610. *status = cmd.resp[0];
  611. return err;
  612. }
  613. #define ERR_NOMEDIUM 3
  614. #define ERR_RETRY 2
  615. #define ERR_ABORT 1
  616. #define ERR_CONTINUE 0
  617. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  618. bool status_valid, u32 status)
  619. {
  620. switch (error) {
  621. case -EILSEQ:
  622. /* response crc error, retry the r/w cmd */
  623. pr_err("%s: %s sending %s command, card status %#x\n",
  624. req->rq_disk->disk_name, "response CRC error",
  625. name, status);
  626. return ERR_RETRY;
  627. case -ETIMEDOUT:
  628. pr_err("%s: %s sending %s command, card status %#x\n",
  629. req->rq_disk->disk_name, "timed out", name, status);
  630. /* If the status cmd initially failed, retry the r/w cmd */
  631. if (!status_valid)
  632. return ERR_RETRY;
  633. /*
  634. * If it was a r/w cmd crc error, or illegal command
  635. * (eg, issued in wrong state) then retry - we should
  636. * have corrected the state problem above.
  637. */
  638. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
  639. return ERR_RETRY;
  640. /* Otherwise abort the command */
  641. return ERR_ABORT;
  642. default:
  643. /* We don't understand the error code the driver gave us */
  644. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  645. req->rq_disk->disk_name, error, status);
  646. return ERR_ABORT;
  647. }
  648. }
  649. /*
  650. * Initial r/w and stop cmd error recovery.
  651. * We don't know whether the card received the r/w cmd or not, so try to
  652. * restore things back to a sane state. Essentially, we do this as follows:
  653. * - Obtain card status. If the first attempt to obtain card status fails,
  654. * the status word will reflect the failed status cmd, not the failed
  655. * r/w cmd. If we fail to obtain card status, it suggests we can no
  656. * longer communicate with the card.
  657. * - Check the card state. If the card received the cmd but there was a
  658. * transient problem with the response, it might still be in a data transfer
  659. * mode. Try to send it a stop command. If this fails, we can't recover.
  660. * - If the r/w cmd failed due to a response CRC error, it was probably
  661. * transient, so retry the cmd.
  662. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  663. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  664. * illegal cmd, retry.
  665. * Otherwise we don't understand what happened, so abort.
  666. */
  667. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  668. struct mmc_blk_request *brq, int *ecc_err)
  669. {
  670. bool prev_cmd_status_valid = true;
  671. u32 status, stop_status = 0;
  672. int err, retry;
  673. if (mmc_card_removed(card))
  674. return ERR_NOMEDIUM;
  675. /*
  676. * Try to get card status which indicates both the card state
  677. * and why there was no response. If the first attempt fails,
  678. * we can't be sure the returned status is for the r/w command.
  679. */
  680. for (retry = 2; retry >= 0; retry--) {
  681. err = get_card_status(card, &status, 0);
  682. if (!err)
  683. break;
  684. prev_cmd_status_valid = false;
  685. pr_err("%s: error %d sending status command, %sing\n",
  686. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  687. }
  688. /* We couldn't get a response from the card. Give up. */
  689. if (err) {
  690. /* Check if the card is removed */
  691. if (mmc_detect_card_removed(card->host))
  692. return ERR_NOMEDIUM;
  693. return ERR_ABORT;
  694. }
  695. /* Flag ECC errors */
  696. if ((status & R1_CARD_ECC_FAILED) ||
  697. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  698. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  699. *ecc_err = 1;
  700. /*
  701. * Check the current card state. If it is in some data transfer
  702. * mode, tell it to stop (and hopefully transition back to TRAN.)
  703. */
  704. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  705. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  706. err = send_stop(card, &stop_status);
  707. if (err)
  708. pr_err("%s: error %d sending stop command\n",
  709. req->rq_disk->disk_name, err);
  710. /*
  711. * If the stop cmd also timed out, the card is probably
  712. * not present, so abort. Other errors are bad news too.
  713. */
  714. if (err)
  715. return ERR_ABORT;
  716. if (stop_status & R1_CARD_ECC_FAILED)
  717. *ecc_err = 1;
  718. }
  719. /* Check for set block count errors */
  720. if (brq->sbc.error)
  721. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  722. prev_cmd_status_valid, status);
  723. /* Check for r/w command errors */
  724. if (brq->cmd.error)
  725. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  726. prev_cmd_status_valid, status);
  727. /* Data errors */
  728. if (!brq->stop.error)
  729. return ERR_CONTINUE;
  730. /* Now for stop errors. These aren't fatal to the transfer. */
  731. pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  732. req->rq_disk->disk_name, brq->stop.error,
  733. brq->cmd.resp[0], status);
  734. /*
  735. * Subsitute in our own stop status as this will give the error
  736. * state which happened during the execution of the r/w command.
  737. */
  738. if (stop_status) {
  739. brq->stop.resp[0] = stop_status;
  740. brq->stop.error = 0;
  741. }
  742. return ERR_CONTINUE;
  743. }
  744. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  745. int type)
  746. {
  747. int err;
  748. if (md->reset_done & type)
  749. return -EEXIST;
  750. md->reset_done |= type;
  751. err = mmc_hw_reset(host);
  752. /* Ensure we switch back to the correct partition */
  753. if (err != -EOPNOTSUPP) {
  754. struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
  755. int part_err;
  756. main_md->part_curr = main_md->part_type;
  757. part_err = mmc_blk_part_switch(host->card, md);
  758. if (part_err) {
  759. /*
  760. * We have failed to get back into the correct
  761. * partition, so we need to abort the whole request.
  762. */
  763. return -ENODEV;
  764. }
  765. }
  766. return err;
  767. }
  768. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  769. {
  770. md->reset_done &= ~type;
  771. }
  772. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  773. {
  774. struct mmc_blk_data *md = mq->data;
  775. struct mmc_card *card = md->queue.card;
  776. unsigned int from, nr, arg;
  777. int err = 0, type = MMC_BLK_DISCARD;
  778. if (!mmc_can_erase(card)) {
  779. err = -EOPNOTSUPP;
  780. goto out;
  781. }
  782. from = blk_rq_pos(req);
  783. nr = blk_rq_sectors(req);
  784. if (mmc_can_discard(card))
  785. arg = MMC_DISCARD_ARG;
  786. else if (mmc_can_trim(card))
  787. arg = MMC_TRIM_ARG;
  788. else
  789. arg = MMC_ERASE_ARG;
  790. retry:
  791. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  792. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  793. INAND_CMD38_ARG_EXT_CSD,
  794. arg == MMC_TRIM_ARG ?
  795. INAND_CMD38_ARG_TRIM :
  796. INAND_CMD38_ARG_ERASE,
  797. 0);
  798. if (err)
  799. goto out;
  800. }
  801. err = mmc_erase(card, from, nr, arg);
  802. out:
  803. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  804. goto retry;
  805. if (!err)
  806. mmc_blk_reset_success(md, type);
  807. blk_end_request(req, err, blk_rq_bytes(req));
  808. return err ? 0 : 1;
  809. }
  810. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  811. struct request *req)
  812. {
  813. struct mmc_blk_data *md = mq->data;
  814. struct mmc_card *card = md->queue.card;
  815. unsigned int from, nr, arg;
  816. int err = 0, type = MMC_BLK_SECDISCARD;
  817. if (!(mmc_can_secure_erase_trim(card))) {
  818. err = -EOPNOTSUPP;
  819. goto out;
  820. }
  821. from = blk_rq_pos(req);
  822. nr = blk_rq_sectors(req);
  823. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  824. arg = MMC_SECURE_TRIM1_ARG;
  825. else
  826. arg = MMC_SECURE_ERASE_ARG;
  827. retry:
  828. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  829. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  830. INAND_CMD38_ARG_EXT_CSD,
  831. arg == MMC_SECURE_TRIM1_ARG ?
  832. INAND_CMD38_ARG_SECTRIM1 :
  833. INAND_CMD38_ARG_SECERASE,
  834. 0);
  835. if (err)
  836. goto out_retry;
  837. }
  838. err = mmc_erase(card, from, nr, arg);
  839. if (err == -EIO)
  840. goto out_retry;
  841. if (err)
  842. goto out;
  843. if (arg == MMC_SECURE_TRIM1_ARG) {
  844. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  845. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  846. INAND_CMD38_ARG_EXT_CSD,
  847. INAND_CMD38_ARG_SECTRIM2,
  848. 0);
  849. if (err)
  850. goto out_retry;
  851. }
  852. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  853. if (err == -EIO)
  854. goto out_retry;
  855. if (err)
  856. goto out;
  857. }
  858. out_retry:
  859. if (err && !mmc_blk_reset(md, card->host, type))
  860. goto retry;
  861. if (!err)
  862. mmc_blk_reset_success(md, type);
  863. out:
  864. blk_end_request(req, err, blk_rq_bytes(req));
  865. return err ? 0 : 1;
  866. }
  867. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  868. {
  869. struct mmc_blk_data *md = mq->data;
  870. struct mmc_card *card = md->queue.card;
  871. int ret = 0;
  872. ret = mmc_flush_cache(card);
  873. if (ret)
  874. ret = -EIO;
  875. blk_end_request_all(req, ret);
  876. return ret ? 0 : 1;
  877. }
  878. /*
  879. * Reformat current write as a reliable write, supporting
  880. * both legacy and the enhanced reliable write MMC cards.
  881. * In each transfer we'll handle only as much as a single
  882. * reliable write can handle, thus finish the request in
  883. * partial completions.
  884. */
  885. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  886. struct mmc_card *card,
  887. struct request *req)
  888. {
  889. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  890. /* Legacy mode imposes restrictions on transfers. */
  891. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  892. brq->data.blocks = 1;
  893. if (brq->data.blocks > card->ext_csd.rel_sectors)
  894. brq->data.blocks = card->ext_csd.rel_sectors;
  895. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  896. brq->data.blocks = 1;
  897. }
  898. }
  899. #define CMD_ERRORS \
  900. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  901. R1_ADDRESS_ERROR | /* Misaligned address */ \
  902. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  903. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  904. R1_CC_ERROR | /* Card controller error */ \
  905. R1_ERROR) /* General/unknown error */
  906. static int mmc_blk_err_check(struct mmc_card *card,
  907. struct mmc_async_req *areq)
  908. {
  909. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  910. mmc_active);
  911. struct mmc_blk_request *brq = &mq_mrq->brq;
  912. struct request *req = mq_mrq->req;
  913. int ecc_err = 0;
  914. /*
  915. * sbc.error indicates a problem with the set block count
  916. * command. No data will have been transferred.
  917. *
  918. * cmd.error indicates a problem with the r/w command. No
  919. * data will have been transferred.
  920. *
  921. * stop.error indicates a problem with the stop command. Data
  922. * may have been transferred, or may still be transferring.
  923. */
  924. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  925. brq->data.error) {
  926. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
  927. case ERR_RETRY:
  928. return MMC_BLK_RETRY;
  929. case ERR_ABORT:
  930. return MMC_BLK_ABORT;
  931. case ERR_NOMEDIUM:
  932. return MMC_BLK_NOMEDIUM;
  933. case ERR_CONTINUE:
  934. break;
  935. }
  936. }
  937. /*
  938. * Check for errors relating to the execution of the
  939. * initial command - such as address errors. No data
  940. * has been transferred.
  941. */
  942. if (brq->cmd.resp[0] & CMD_ERRORS) {
  943. pr_err("%s: r/w command failed, status = %#x\n",
  944. req->rq_disk->disk_name, brq->cmd.resp[0]);
  945. return MMC_BLK_ABORT;
  946. }
  947. /*
  948. * Everything else is either success, or a data error of some
  949. * kind. If it was a write, we may have transitioned to
  950. * program mode, which we have to wait for it to complete.
  951. */
  952. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  953. u32 status;
  954. unsigned long timeout;
  955. timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
  956. do {
  957. int err = get_card_status(card, &status, 5);
  958. if (err) {
  959. pr_err("%s: error %d requesting status\n",
  960. req->rq_disk->disk_name, err);
  961. return MMC_BLK_CMD_ERR;
  962. }
  963. /* Timeout if the device never becomes ready for data
  964. * and never leaves the program state.
  965. */
  966. if (time_after(jiffies, timeout)) {
  967. pr_err("%s: Card stuck in programming state!"\
  968. " %s %s\n", mmc_hostname(card->host),
  969. req->rq_disk->disk_name, __func__);
  970. return MMC_BLK_CMD_ERR;
  971. }
  972. /*
  973. * Some cards mishandle the status bits,
  974. * so make sure to check both the busy
  975. * indication and the card state.
  976. */
  977. } while (!(status & R1_READY_FOR_DATA) ||
  978. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  979. }
  980. if (brq->data.error) {
  981. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  982. req->rq_disk->disk_name, brq->data.error,
  983. (unsigned)blk_rq_pos(req),
  984. (unsigned)blk_rq_sectors(req),
  985. brq->cmd.resp[0], brq->stop.resp[0]);
  986. if (rq_data_dir(req) == READ) {
  987. if (ecc_err)
  988. return MMC_BLK_ECC_ERR;
  989. return MMC_BLK_DATA_ERR;
  990. } else {
  991. return MMC_BLK_CMD_ERR;
  992. }
  993. }
  994. if (!brq->data.bytes_xfered)
  995. return MMC_BLK_RETRY;
  996. if (mmc_packed_cmd(mq_mrq->cmd_type)) {
  997. if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
  998. return MMC_BLK_PARTIAL;
  999. else
  1000. return MMC_BLK_SUCCESS;
  1001. }
  1002. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1003. return MMC_BLK_PARTIAL;
  1004. return MMC_BLK_SUCCESS;
  1005. }
  1006. static int mmc_blk_packed_err_check(struct mmc_card *card,
  1007. struct mmc_async_req *areq)
  1008. {
  1009. struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
  1010. mmc_active);
  1011. struct request *req = mq_rq->req;
  1012. struct mmc_packed *packed = mq_rq->packed;
  1013. int err, check, status;
  1014. u8 *ext_csd;
  1015. BUG_ON(!packed);
  1016. packed->retries--;
  1017. check = mmc_blk_err_check(card, areq);
  1018. err = get_card_status(card, &status, 0);
  1019. if (err) {
  1020. pr_err("%s: error %d sending status command\n",
  1021. req->rq_disk->disk_name, err);
  1022. return MMC_BLK_ABORT;
  1023. }
  1024. if (status & R1_EXCEPTION_EVENT) {
  1025. ext_csd = kzalloc(512, GFP_KERNEL);
  1026. if (!ext_csd) {
  1027. pr_err("%s: unable to allocate buffer for ext_csd\n",
  1028. req->rq_disk->disk_name);
  1029. return -ENOMEM;
  1030. }
  1031. err = mmc_send_ext_csd(card, ext_csd);
  1032. if (err) {
  1033. pr_err("%s: error %d sending ext_csd\n",
  1034. req->rq_disk->disk_name, err);
  1035. check = MMC_BLK_ABORT;
  1036. goto free;
  1037. }
  1038. if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
  1039. EXT_CSD_PACKED_FAILURE) &&
  1040. (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1041. EXT_CSD_PACKED_GENERIC_ERROR)) {
  1042. if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1043. EXT_CSD_PACKED_INDEXED_ERROR) {
  1044. packed->idx_failure =
  1045. ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
  1046. check = MMC_BLK_PARTIAL;
  1047. }
  1048. pr_err("%s: packed cmd failed, nr %u, sectors %u, "
  1049. "failure index: %d\n",
  1050. req->rq_disk->disk_name, packed->nr_entries,
  1051. packed->blocks, packed->idx_failure);
  1052. }
  1053. free:
  1054. kfree(ext_csd);
  1055. }
  1056. return check;
  1057. }
  1058. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1059. struct mmc_card *card,
  1060. int disable_multi,
  1061. struct mmc_queue *mq)
  1062. {
  1063. u32 readcmd, writecmd;
  1064. struct mmc_blk_request *brq = &mqrq->brq;
  1065. struct request *req = mqrq->req;
  1066. struct mmc_blk_data *md = mq->data;
  1067. bool do_data_tag;
  1068. /*
  1069. * Reliable writes are used to implement Forced Unit Access and
  1070. * REQ_META accesses, and are supported only on MMCs.
  1071. *
  1072. * XXX: this really needs a good explanation of why REQ_META
  1073. * is treated special.
  1074. */
  1075. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  1076. (req->cmd_flags & REQ_META)) &&
  1077. (rq_data_dir(req) == WRITE) &&
  1078. (md->flags & MMC_BLK_REL_WR);
  1079. memset(brq, 0, sizeof(struct mmc_blk_request));
  1080. brq->mrq.cmd = &brq->cmd;
  1081. brq->mrq.data = &brq->data;
  1082. brq->cmd.arg = blk_rq_pos(req);
  1083. if (!mmc_card_blockaddr(card))
  1084. brq->cmd.arg <<= 9;
  1085. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1086. brq->data.blksz = 512;
  1087. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1088. brq->stop.arg = 0;
  1089. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1090. brq->data.blocks = blk_rq_sectors(req);
  1091. /*
  1092. * The block layer doesn't support all sector count
  1093. * restrictions, so we need to be prepared for too big
  1094. * requests.
  1095. */
  1096. if (brq->data.blocks > card->host->max_blk_count)
  1097. brq->data.blocks = card->host->max_blk_count;
  1098. if (brq->data.blocks > 1) {
  1099. /*
  1100. * After a read error, we redo the request one sector
  1101. * at a time in order to accurately determine which
  1102. * sectors can be read successfully.
  1103. */
  1104. if (disable_multi)
  1105. brq->data.blocks = 1;
  1106. /* Some controllers can't do multiblock reads due to hw bugs */
  1107. if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
  1108. rq_data_dir(req) == READ)
  1109. brq->data.blocks = 1;
  1110. }
  1111. if (brq->data.blocks > 1 || do_rel_wr) {
  1112. /* SPI multiblock writes terminate using a special
  1113. * token, not a STOP_TRANSMISSION request.
  1114. */
  1115. if (!mmc_host_is_spi(card->host) ||
  1116. rq_data_dir(req) == READ)
  1117. brq->mrq.stop = &brq->stop;
  1118. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1119. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1120. } else {
  1121. brq->mrq.stop = NULL;
  1122. readcmd = MMC_READ_SINGLE_BLOCK;
  1123. writecmd = MMC_WRITE_BLOCK;
  1124. }
  1125. if (rq_data_dir(req) == READ) {
  1126. brq->cmd.opcode = readcmd;
  1127. brq->data.flags |= MMC_DATA_READ;
  1128. } else {
  1129. brq->cmd.opcode = writecmd;
  1130. brq->data.flags |= MMC_DATA_WRITE;
  1131. }
  1132. if (do_rel_wr)
  1133. mmc_apply_rel_rw(brq, card, req);
  1134. /*
  1135. * Data tag is used only during writing meta data to speed
  1136. * up write and any subsequent read of this meta data
  1137. */
  1138. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1139. (req->cmd_flags & REQ_META) &&
  1140. (rq_data_dir(req) == WRITE) &&
  1141. ((brq->data.blocks * brq->data.blksz) >=
  1142. card->ext_csd.data_tag_unit_size);
  1143. /*
  1144. * Pre-defined multi-block transfers are preferable to
  1145. * open ended-ones (and necessary for reliable writes).
  1146. * However, it is not sufficient to just send CMD23,
  1147. * and avoid the final CMD12, as on an error condition
  1148. * CMD12 (stop) needs to be sent anyway. This, coupled
  1149. * with Auto-CMD23 enhancements provided by some
  1150. * hosts, means that the complexity of dealing
  1151. * with this is best left to the host. If CMD23 is
  1152. * supported by card and host, we'll fill sbc in and let
  1153. * the host deal with handling it correctly. This means
  1154. * that for hosts that don't expose MMC_CAP_CMD23, no
  1155. * change of behavior will be observed.
  1156. *
  1157. * N.B: Some MMC cards experience perf degradation.
  1158. * We'll avoid using CMD23-bounded multiblock writes for
  1159. * these, while retaining features like reliable writes.
  1160. */
  1161. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1162. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1163. do_data_tag)) {
  1164. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1165. brq->sbc.arg = brq->data.blocks |
  1166. (do_rel_wr ? (1 << 31) : 0) |
  1167. (do_data_tag ? (1 << 29) : 0);
  1168. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1169. brq->mrq.sbc = &brq->sbc;
  1170. }
  1171. mmc_set_data_timeout(&brq->data, card);
  1172. brq->data.sg = mqrq->sg;
  1173. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1174. /*
  1175. * Adjust the sg list so it is the same size as the
  1176. * request.
  1177. */
  1178. if (brq->data.blocks != blk_rq_sectors(req)) {
  1179. int i, data_size = brq->data.blocks << 9;
  1180. struct scatterlist *sg;
  1181. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1182. data_size -= sg->length;
  1183. if (data_size <= 0) {
  1184. sg->length += data_size;
  1185. i++;
  1186. break;
  1187. }
  1188. }
  1189. brq->data.sg_len = i;
  1190. }
  1191. mqrq->mmc_active.mrq = &brq->mrq;
  1192. mqrq->mmc_active.err_check = mmc_blk_err_check;
  1193. mmc_queue_bounce_pre(mqrq);
  1194. }
  1195. static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
  1196. struct mmc_card *card)
  1197. {
  1198. unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
  1199. unsigned int max_seg_sz = queue_max_segment_size(q);
  1200. unsigned int len, nr_segs = 0;
  1201. do {
  1202. len = min(hdr_sz, max_seg_sz);
  1203. hdr_sz -= len;
  1204. nr_segs++;
  1205. } while (hdr_sz);
  1206. return nr_segs;
  1207. }
  1208. static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
  1209. {
  1210. struct request_queue *q = mq->queue;
  1211. struct mmc_card *card = mq->card;
  1212. struct request *cur = req, *next = NULL;
  1213. struct mmc_blk_data *md = mq->data;
  1214. struct mmc_queue_req *mqrq = mq->mqrq_cur;
  1215. bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
  1216. unsigned int req_sectors = 0, phys_segments = 0;
  1217. unsigned int max_blk_count, max_phys_segs;
  1218. bool put_back = true;
  1219. u8 max_packed_rw = 0;
  1220. u8 reqs = 0;
  1221. if (!(md->flags & MMC_BLK_PACKED_CMD))
  1222. goto no_packed;
  1223. if ((rq_data_dir(cur) == WRITE) &&
  1224. mmc_host_packed_wr(card->host))
  1225. max_packed_rw = card->ext_csd.max_packed_writes;
  1226. if (max_packed_rw == 0)
  1227. goto no_packed;
  1228. if (mmc_req_rel_wr(cur) &&
  1229. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1230. goto no_packed;
  1231. if (mmc_large_sector(card) &&
  1232. !IS_ALIGNED(blk_rq_sectors(cur), 8))
  1233. goto no_packed;
  1234. mmc_blk_clear_packed(mqrq);
  1235. max_blk_count = min(card->host->max_blk_count,
  1236. card->host->max_req_size >> 9);
  1237. if (unlikely(max_blk_count > 0xffff))
  1238. max_blk_count = 0xffff;
  1239. max_phys_segs = queue_max_segments(q);
  1240. req_sectors += blk_rq_sectors(cur);
  1241. phys_segments += cur->nr_phys_segments;
  1242. if (rq_data_dir(cur) == WRITE) {
  1243. req_sectors += mmc_large_sector(card) ? 8 : 1;
  1244. phys_segments += mmc_calc_packed_hdr_segs(q, card);
  1245. }
  1246. do {
  1247. if (reqs >= max_packed_rw - 1) {
  1248. put_back = false;
  1249. break;
  1250. }
  1251. spin_lock_irq(q->queue_lock);
  1252. next = blk_fetch_request(q);
  1253. spin_unlock_irq(q->queue_lock);
  1254. if (!next) {
  1255. put_back = false;
  1256. break;
  1257. }
  1258. if (mmc_large_sector(card) &&
  1259. !IS_ALIGNED(blk_rq_sectors(next), 8))
  1260. break;
  1261. if (next->cmd_flags & REQ_DISCARD ||
  1262. next->cmd_flags & REQ_FLUSH)
  1263. break;
  1264. if (rq_data_dir(cur) != rq_data_dir(next))
  1265. break;
  1266. if (mmc_req_rel_wr(next) &&
  1267. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1268. break;
  1269. req_sectors += blk_rq_sectors(next);
  1270. if (req_sectors > max_blk_count)
  1271. break;
  1272. phys_segments += next->nr_phys_segments;
  1273. if (phys_segments > max_phys_segs)
  1274. break;
  1275. list_add_tail(&next->queuelist, &mqrq->packed->list);
  1276. cur = next;
  1277. reqs++;
  1278. } while (1);
  1279. if (put_back) {
  1280. spin_lock_irq(q->queue_lock);
  1281. blk_requeue_request(q, next);
  1282. spin_unlock_irq(q->queue_lock);
  1283. }
  1284. if (reqs > 0) {
  1285. list_add(&req->queuelist, &mqrq->packed->list);
  1286. mqrq->packed->nr_entries = ++reqs;
  1287. mqrq->packed->retries = reqs;
  1288. return reqs;
  1289. }
  1290. no_packed:
  1291. mqrq->cmd_type = MMC_PACKED_NONE;
  1292. return 0;
  1293. }
  1294. static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
  1295. struct mmc_card *card,
  1296. struct mmc_queue *mq)
  1297. {
  1298. struct mmc_blk_request *brq = &mqrq->brq;
  1299. struct request *req = mqrq->req;
  1300. struct request *prq;
  1301. struct mmc_blk_data *md = mq->data;
  1302. struct mmc_packed *packed = mqrq->packed;
  1303. bool do_rel_wr, do_data_tag;
  1304. u32 *packed_cmd_hdr;
  1305. u8 hdr_blocks;
  1306. u8 i = 1;
  1307. BUG_ON(!packed);
  1308. mqrq->cmd_type = MMC_PACKED_WRITE;
  1309. packed->blocks = 0;
  1310. packed->idx_failure = MMC_PACKED_NR_IDX;
  1311. packed_cmd_hdr = packed->cmd_hdr;
  1312. memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
  1313. packed_cmd_hdr[0] = (packed->nr_entries << 16) |
  1314. (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
  1315. hdr_blocks = mmc_large_sector(card) ? 8 : 1;
  1316. /*
  1317. * Argument for each entry of packed group
  1318. */
  1319. list_for_each_entry(prq, &packed->list, queuelist) {
  1320. do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
  1321. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1322. (prq->cmd_flags & REQ_META) &&
  1323. (rq_data_dir(prq) == WRITE) &&
  1324. ((brq->data.blocks * brq->data.blksz) >=
  1325. card->ext_csd.data_tag_unit_size);
  1326. /* Argument of CMD23 */
  1327. packed_cmd_hdr[(i * 2)] =
  1328. (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
  1329. (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
  1330. blk_rq_sectors(prq);
  1331. /* Argument of CMD18 or CMD25 */
  1332. packed_cmd_hdr[((i * 2)) + 1] =
  1333. mmc_card_blockaddr(card) ?
  1334. blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
  1335. packed->blocks += blk_rq_sectors(prq);
  1336. i++;
  1337. }
  1338. memset(brq, 0, sizeof(struct mmc_blk_request));
  1339. brq->mrq.cmd = &brq->cmd;
  1340. brq->mrq.data = &brq->data;
  1341. brq->mrq.sbc = &brq->sbc;
  1342. brq->mrq.stop = &brq->stop;
  1343. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1344. brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
  1345. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1346. brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
  1347. brq->cmd.arg = blk_rq_pos(req);
  1348. if (!mmc_card_blockaddr(card))
  1349. brq->cmd.arg <<= 9;
  1350. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1351. brq->data.blksz = 512;
  1352. brq->data.blocks = packed->blocks + hdr_blocks;
  1353. brq->data.flags |= MMC_DATA_WRITE;
  1354. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1355. brq->stop.arg = 0;
  1356. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1357. mmc_set_data_timeout(&brq->data, card);
  1358. brq->data.sg = mqrq->sg;
  1359. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1360. mqrq->mmc_active.mrq = &brq->mrq;
  1361. mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
  1362. mmc_queue_bounce_pre(mqrq);
  1363. }
  1364. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  1365. struct mmc_blk_request *brq, struct request *req,
  1366. int ret)
  1367. {
  1368. struct mmc_queue_req *mq_rq;
  1369. mq_rq = container_of(brq, struct mmc_queue_req, brq);
  1370. /*
  1371. * If this is an SD card and we're writing, we can first
  1372. * mark the known good sectors as ok.
  1373. *
  1374. * If the card is not SD, we can still ok written sectors
  1375. * as reported by the controller (which might be less than
  1376. * the real number of written sectors, but never more).
  1377. */
  1378. if (mmc_card_sd(card)) {
  1379. u32 blocks;
  1380. blocks = mmc_sd_num_wr_blocks(card);
  1381. if (blocks != (u32)-1) {
  1382. ret = blk_end_request(req, 0, blocks << 9);
  1383. }
  1384. } else {
  1385. if (!mmc_packed_cmd(mq_rq->cmd_type))
  1386. ret = blk_end_request(req, 0, brq->data.bytes_xfered);
  1387. }
  1388. return ret;
  1389. }
  1390. static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
  1391. {
  1392. struct request *prq;
  1393. struct mmc_packed *packed = mq_rq->packed;
  1394. int idx = packed->idx_failure, i = 0;
  1395. int ret = 0;
  1396. BUG_ON(!packed);
  1397. while (!list_empty(&packed->list)) {
  1398. prq = list_entry_rq(packed->list.next);
  1399. if (idx == i) {
  1400. /* retry from error index */
  1401. packed->nr_entries -= idx;
  1402. mq_rq->req = prq;
  1403. ret = 1;
  1404. if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
  1405. list_del_init(&prq->queuelist);
  1406. mmc_blk_clear_packed(mq_rq);
  1407. }
  1408. return ret;
  1409. }
  1410. list_del_init(&prq->queuelist);
  1411. blk_end_request(prq, 0, blk_rq_bytes(prq));
  1412. i++;
  1413. }
  1414. mmc_blk_clear_packed(mq_rq);
  1415. return ret;
  1416. }
  1417. static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
  1418. {
  1419. struct request *prq;
  1420. struct mmc_packed *packed = mq_rq->packed;
  1421. BUG_ON(!packed);
  1422. while (!list_empty(&packed->list)) {
  1423. prq = list_entry_rq(packed->list.next);
  1424. list_del_init(&prq->queuelist);
  1425. blk_end_request(prq, -EIO, blk_rq_bytes(prq));
  1426. }
  1427. mmc_blk_clear_packed(mq_rq);
  1428. }
  1429. static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
  1430. struct mmc_queue_req *mq_rq)
  1431. {
  1432. struct request *prq;
  1433. struct request_queue *q = mq->queue;
  1434. struct mmc_packed *packed = mq_rq->packed;
  1435. BUG_ON(!packed);
  1436. while (!list_empty(&packed->list)) {
  1437. prq = list_entry_rq(packed->list.prev);
  1438. if (prq->queuelist.prev != &packed->list) {
  1439. list_del_init(&prq->queuelist);
  1440. spin_lock_irq(q->queue_lock);
  1441. blk_requeue_request(mq->queue, prq);
  1442. spin_unlock_irq(q->queue_lock);
  1443. } else {
  1444. list_del_init(&prq->queuelist);
  1445. }
  1446. }
  1447. mmc_blk_clear_packed(mq_rq);
  1448. }
  1449. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  1450. {
  1451. struct mmc_blk_data *md = mq->data;
  1452. struct mmc_card *card = md->queue.card;
  1453. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  1454. int ret = 1, disable_multi = 0, retry = 0, type;
  1455. enum mmc_blk_status status;
  1456. struct mmc_queue_req *mq_rq;
  1457. struct request *req = rqc;
  1458. struct mmc_async_req *areq;
  1459. const u8 packed_nr = 2;
  1460. u8 reqs = 0;
  1461. if (!rqc && !mq->mqrq_prev->req)
  1462. return 0;
  1463. if (rqc)
  1464. reqs = mmc_blk_prep_packed_list(mq, rqc);
  1465. do {
  1466. if (rqc) {
  1467. /*
  1468. * When 4KB native sector is enabled, only 8 blocks
  1469. * multiple read or write is allowed
  1470. */
  1471. if ((brq->data.blocks & 0x07) &&
  1472. (card->ext_csd.data_sector_size == 4096)) {
  1473. pr_err("%s: Transfer size is not 4KB sector size aligned\n",
  1474. req->rq_disk->disk_name);
  1475. mq_rq = mq->mqrq_cur;
  1476. goto cmd_abort;
  1477. }
  1478. if (reqs >= packed_nr)
  1479. mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
  1480. card, mq);
  1481. else
  1482. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1483. areq = &mq->mqrq_cur->mmc_active;
  1484. } else
  1485. areq = NULL;
  1486. areq = mmc_start_req(card->host, areq, (int *) &status);
  1487. if (!areq) {
  1488. if (status == MMC_BLK_NEW_REQUEST)
  1489. mq->flags |= MMC_QUEUE_NEW_REQUEST;
  1490. return 0;
  1491. }
  1492. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  1493. brq = &mq_rq->brq;
  1494. req = mq_rq->req;
  1495. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1496. mmc_queue_bounce_post(mq_rq);
  1497. switch (status) {
  1498. case MMC_BLK_SUCCESS:
  1499. case MMC_BLK_PARTIAL:
  1500. /*
  1501. * A block was successfully transferred.
  1502. */
  1503. mmc_blk_reset_success(md, type);
  1504. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1505. ret = mmc_blk_end_packed_req(mq_rq);
  1506. break;
  1507. } else {
  1508. ret = blk_end_request(req, 0,
  1509. brq->data.bytes_xfered);
  1510. }
  1511. /*
  1512. * If the blk_end_request function returns non-zero even
  1513. * though all data has been transferred and no errors
  1514. * were returned by the host controller, it's a bug.
  1515. */
  1516. if (status == MMC_BLK_SUCCESS && ret) {
  1517. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1518. __func__, blk_rq_bytes(req),
  1519. brq->data.bytes_xfered);
  1520. rqc = NULL;
  1521. goto cmd_abort;
  1522. }
  1523. break;
  1524. case MMC_BLK_CMD_ERR:
  1525. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1526. if (!mmc_blk_reset(md, card->host, type))
  1527. break;
  1528. goto cmd_abort;
  1529. case MMC_BLK_RETRY:
  1530. if (retry++ < 5)
  1531. break;
  1532. /* Fall through */
  1533. case MMC_BLK_ABORT:
  1534. if (!mmc_blk_reset(md, card->host, type))
  1535. break;
  1536. goto cmd_abort;
  1537. case MMC_BLK_DATA_ERR: {
  1538. int err;
  1539. err = mmc_blk_reset(md, card->host, type);
  1540. if (!err)
  1541. break;
  1542. if (err == -ENODEV ||
  1543. mmc_packed_cmd(mq_rq->cmd_type))
  1544. goto cmd_abort;
  1545. /* Fall through */
  1546. }
  1547. case MMC_BLK_ECC_ERR:
  1548. if (brq->data.blocks > 1) {
  1549. /* Redo read one sector at a time */
  1550. pr_warning("%s: retrying using single block read\n",
  1551. req->rq_disk->disk_name);
  1552. disable_multi = 1;
  1553. break;
  1554. }
  1555. /*
  1556. * After an error, we redo I/O one sector at a
  1557. * time, so we only reach here after trying to
  1558. * read a single sector.
  1559. */
  1560. ret = blk_end_request(req, -EIO,
  1561. brq->data.blksz);
  1562. if (!ret)
  1563. goto start_new_req;
  1564. break;
  1565. case MMC_BLK_NOMEDIUM:
  1566. goto cmd_abort;
  1567. default:
  1568. pr_err("%s: Unhandled return value (%d)",
  1569. req->rq_disk->disk_name, status);
  1570. goto cmd_abort;
  1571. }
  1572. if (ret) {
  1573. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1574. if (!mq_rq->packed->retries)
  1575. goto cmd_abort;
  1576. mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
  1577. mmc_start_req(card->host,
  1578. &mq_rq->mmc_active, NULL);
  1579. } else {
  1580. /*
  1581. * In case of a incomplete request
  1582. * prepare it again and resend.
  1583. */
  1584. mmc_blk_rw_rq_prep(mq_rq, card,
  1585. disable_multi, mq);
  1586. mmc_start_req(card->host,
  1587. &mq_rq->mmc_active, NULL);
  1588. }
  1589. }
  1590. } while (ret);
  1591. return 1;
  1592. cmd_abort:
  1593. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1594. mmc_blk_abort_packed_req(mq_rq);
  1595. } else {
  1596. if (mmc_card_removed(card))
  1597. req->cmd_flags |= REQ_QUIET;
  1598. while (ret)
  1599. ret = blk_end_request(req, -EIO,
  1600. blk_rq_cur_bytes(req));
  1601. }
  1602. start_new_req:
  1603. if (rqc) {
  1604. if (mmc_card_removed(card)) {
  1605. rqc->cmd_flags |= REQ_QUIET;
  1606. blk_end_request_all(rqc, -EIO);
  1607. } else {
  1608. /*
  1609. * If current request is packed, it needs to put back.
  1610. */
  1611. if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
  1612. mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
  1613. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1614. mmc_start_req(card->host,
  1615. &mq->mqrq_cur->mmc_active, NULL);
  1616. }
  1617. }
  1618. return 0;
  1619. }
  1620. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1621. {
  1622. int ret;
  1623. struct mmc_blk_data *md = mq->data;
  1624. struct mmc_card *card = md->queue.card;
  1625. struct mmc_host *host = card->host;
  1626. unsigned long flags;
  1627. if (req && !mq->mqrq_prev->req)
  1628. /* claim host only for the first request */
  1629. mmc_claim_host(card->host);
  1630. ret = mmc_blk_part_switch(card, md);
  1631. if (ret) {
  1632. if (req) {
  1633. blk_end_request_all(req, -EIO);
  1634. }
  1635. ret = 0;
  1636. goto out;
  1637. }
  1638. mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
  1639. if (req && req->cmd_flags & REQ_DISCARD) {
  1640. /* complete ongoing async transfer before issuing discard */
  1641. if (card->host->areq)
  1642. mmc_blk_issue_rw_rq(mq, NULL);
  1643. if (req->cmd_flags & REQ_SECURE &&
  1644. !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
  1645. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1646. else
  1647. ret = mmc_blk_issue_discard_rq(mq, req);
  1648. } else if (req && req->cmd_flags & REQ_FLUSH) {
  1649. /* complete ongoing async transfer before issuing flush */
  1650. if (card->host->areq)
  1651. mmc_blk_issue_rw_rq(mq, NULL);
  1652. ret = mmc_blk_issue_flush(mq, req);
  1653. } else {
  1654. if (!req && host->areq) {
  1655. spin_lock_irqsave(&host->context_info.lock, flags);
  1656. host->context_info.is_waiting_last_req = true;
  1657. spin_unlock_irqrestore(&host->context_info.lock, flags);
  1658. }
  1659. ret = mmc_blk_issue_rw_rq(mq, req);
  1660. }
  1661. out:
  1662. if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
  1663. (req && (req->cmd_flags & MMC_REQ_SPECIAL_MASK)))
  1664. /*
  1665. * Release host when there are no more requests
  1666. * and after special request(discard, flush) is done.
  1667. * In case sepecial request, there is no reentry to
  1668. * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
  1669. */
  1670. mmc_release_host(card->host);
  1671. return ret;
  1672. }
  1673. static inline int mmc_blk_readonly(struct mmc_card *card)
  1674. {
  1675. return mmc_card_readonly(card) ||
  1676. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1677. }
  1678. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1679. struct device *parent,
  1680. sector_t size,
  1681. bool default_ro,
  1682. const char *subname,
  1683. int area_type)
  1684. {
  1685. struct mmc_blk_data *md;
  1686. int devidx, ret;
  1687. devidx = find_first_zero_bit(dev_use, max_devices);
  1688. if (devidx >= max_devices)
  1689. return ERR_PTR(-ENOSPC);
  1690. __set_bit(devidx, dev_use);
  1691. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1692. if (!md) {
  1693. ret = -ENOMEM;
  1694. goto out;
  1695. }
  1696. /*
  1697. * !subname implies we are creating main mmc_blk_data that will be
  1698. * associated with mmc_card with mmc_set_drvdata. Due to device
  1699. * partitions, devidx will not coincide with a per-physical card
  1700. * index anymore so we keep track of a name index.
  1701. */
  1702. if (!subname) {
  1703. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1704. __set_bit(md->name_idx, name_use);
  1705. } else
  1706. md->name_idx = ((struct mmc_blk_data *)
  1707. dev_to_disk(parent)->private_data)->name_idx;
  1708. md->area_type = area_type;
  1709. /*
  1710. * Set the read-only status based on the supported commands
  1711. * and the write protect switch.
  1712. */
  1713. md->read_only = mmc_blk_readonly(card);
  1714. md->disk = alloc_disk(perdev_minors);
  1715. if (md->disk == NULL) {
  1716. ret = -ENOMEM;
  1717. goto err_kfree;
  1718. }
  1719. spin_lock_init(&md->lock);
  1720. INIT_LIST_HEAD(&md->part);
  1721. md->usage = 1;
  1722. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1723. if (ret)
  1724. goto err_putdisk;
  1725. md->queue.issue_fn = mmc_blk_issue_rq;
  1726. md->queue.data = md;
  1727. md->disk->major = MMC_BLOCK_MAJOR;
  1728. md->disk->first_minor = devidx * perdev_minors;
  1729. md->disk->fops = &mmc_bdops;
  1730. md->disk->private_data = md;
  1731. md->disk->queue = md->queue.queue;
  1732. md->disk->driverfs_dev = parent;
  1733. set_disk_ro(md->disk, md->read_only || default_ro);
  1734. if (area_type & MMC_BLK_DATA_AREA_RPMB)
  1735. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  1736. /*
  1737. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1738. *
  1739. * - be set for removable media with permanent block devices
  1740. * - be unset for removable block devices with permanent media
  1741. *
  1742. * Since MMC block devices clearly fall under the second
  1743. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1744. * should use the block device creation/destruction hotplug
  1745. * messages to tell when the card is present.
  1746. */
  1747. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1748. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1749. if (mmc_card_mmc(card))
  1750. blk_queue_logical_block_size(md->queue.queue,
  1751. card->ext_csd.data_sector_size);
  1752. else
  1753. blk_queue_logical_block_size(md->queue.queue, 512);
  1754. set_capacity(md->disk, size);
  1755. if (mmc_host_cmd23(card->host)) {
  1756. if (mmc_card_mmc(card) ||
  1757. (mmc_card_sd(card) &&
  1758. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1759. md->flags |= MMC_BLK_CMD23;
  1760. }
  1761. if (mmc_card_mmc(card) &&
  1762. md->flags & MMC_BLK_CMD23 &&
  1763. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1764. card->ext_csd.rel_sectors)) {
  1765. md->flags |= MMC_BLK_REL_WR;
  1766. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1767. }
  1768. if (mmc_card_mmc(card) &&
  1769. (area_type == MMC_BLK_DATA_AREA_MAIN) &&
  1770. (md->flags & MMC_BLK_CMD23) &&
  1771. card->ext_csd.packed_event_en) {
  1772. if (!mmc_packed_init(&md->queue, card))
  1773. md->flags |= MMC_BLK_PACKED_CMD;
  1774. }
  1775. return md;
  1776. err_putdisk:
  1777. put_disk(md->disk);
  1778. err_kfree:
  1779. kfree(md);
  1780. out:
  1781. return ERR_PTR(ret);
  1782. }
  1783. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1784. {
  1785. sector_t size;
  1786. struct mmc_blk_data *md;
  1787. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1788. /*
  1789. * The EXT_CSD sector count is in number or 512 byte
  1790. * sectors.
  1791. */
  1792. size = card->ext_csd.sectors;
  1793. } else {
  1794. /*
  1795. * The CSD capacity field is in units of read_blkbits.
  1796. * set_capacity takes units of 512 bytes.
  1797. */
  1798. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1799. }
  1800. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  1801. MMC_BLK_DATA_AREA_MAIN);
  1802. return md;
  1803. }
  1804. static int mmc_blk_alloc_part(struct mmc_card *card,
  1805. struct mmc_blk_data *md,
  1806. unsigned int part_type,
  1807. sector_t size,
  1808. bool default_ro,
  1809. const char *subname,
  1810. int area_type)
  1811. {
  1812. char cap_str[10];
  1813. struct mmc_blk_data *part_md;
  1814. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1815. subname, area_type);
  1816. if (IS_ERR(part_md))
  1817. return PTR_ERR(part_md);
  1818. part_md->part_type = part_type;
  1819. list_add(&part_md->part, &md->part);
  1820. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1821. cap_str, sizeof(cap_str));
  1822. pr_info("%s: %s %s partition %u %s\n",
  1823. part_md->disk->disk_name, mmc_card_id(card),
  1824. mmc_card_name(card), part_md->part_type, cap_str);
  1825. return 0;
  1826. }
  1827. /* MMC Physical partitions consist of two boot partitions and
  1828. * up to four general purpose partitions.
  1829. * For each partition enabled in EXT_CSD a block device will be allocatedi
  1830. * to provide access to the partition.
  1831. */
  1832. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1833. {
  1834. int idx, ret = 0;
  1835. if (!mmc_card_mmc(card))
  1836. return 0;
  1837. for (idx = 0; idx < card->nr_parts; idx++) {
  1838. if (card->part[idx].size) {
  1839. ret = mmc_blk_alloc_part(card, md,
  1840. card->part[idx].part_cfg,
  1841. card->part[idx].size >> 9,
  1842. card->part[idx].force_ro,
  1843. card->part[idx].name,
  1844. card->part[idx].area_type);
  1845. if (ret)
  1846. return ret;
  1847. }
  1848. }
  1849. return ret;
  1850. }
  1851. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1852. {
  1853. struct mmc_card *card;
  1854. if (md) {
  1855. card = md->queue.card;
  1856. if (md->disk->flags & GENHD_FL_UP) {
  1857. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1858. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1859. card->ext_csd.boot_ro_lockable)
  1860. device_remove_file(disk_to_dev(md->disk),
  1861. &md->power_ro_lock);
  1862. /* Stop new requests from getting into the queue */
  1863. del_gendisk(md->disk);
  1864. }
  1865. /* Then flush out any already in there */
  1866. mmc_cleanup_queue(&md->queue);
  1867. if (md->flags & MMC_BLK_PACKED_CMD)
  1868. mmc_packed_clean(&md->queue);
  1869. mmc_blk_put(md);
  1870. }
  1871. }
  1872. static void mmc_blk_remove_parts(struct mmc_card *card,
  1873. struct mmc_blk_data *md)
  1874. {
  1875. struct list_head *pos, *q;
  1876. struct mmc_blk_data *part_md;
  1877. __clear_bit(md->name_idx, name_use);
  1878. list_for_each_safe(pos, q, &md->part) {
  1879. part_md = list_entry(pos, struct mmc_blk_data, part);
  1880. list_del(pos);
  1881. mmc_blk_remove_req(part_md);
  1882. }
  1883. }
  1884. static int mmc_add_disk(struct mmc_blk_data *md)
  1885. {
  1886. int ret;
  1887. struct mmc_card *card = md->queue.card;
  1888. add_disk(md->disk);
  1889. md->force_ro.show = force_ro_show;
  1890. md->force_ro.store = force_ro_store;
  1891. sysfs_attr_init(&md->force_ro.attr);
  1892. md->force_ro.attr.name = "force_ro";
  1893. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1894. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1895. if (ret)
  1896. goto force_ro_fail;
  1897. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1898. card->ext_csd.boot_ro_lockable) {
  1899. umode_t mode;
  1900. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  1901. mode = S_IRUGO;
  1902. else
  1903. mode = S_IRUGO | S_IWUSR;
  1904. md->power_ro_lock.show = power_ro_lock_show;
  1905. md->power_ro_lock.store = power_ro_lock_store;
  1906. sysfs_attr_init(&md->power_ro_lock.attr);
  1907. md->power_ro_lock.attr.mode = mode;
  1908. md->power_ro_lock.attr.name =
  1909. "ro_lock_until_next_power_on";
  1910. ret = device_create_file(disk_to_dev(md->disk),
  1911. &md->power_ro_lock);
  1912. if (ret)
  1913. goto power_ro_lock_fail;
  1914. }
  1915. return ret;
  1916. power_ro_lock_fail:
  1917. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1918. force_ro_fail:
  1919. del_gendisk(md->disk);
  1920. return ret;
  1921. }
  1922. #define CID_MANFID_SANDISK 0x2
  1923. #define CID_MANFID_TOSHIBA 0x11
  1924. #define CID_MANFID_MICRON 0x13
  1925. #define CID_MANFID_SAMSUNG 0x15
  1926. static const struct mmc_fixup blk_fixups[] =
  1927. {
  1928. MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1929. MMC_QUIRK_INAND_CMD38),
  1930. MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1931. MMC_QUIRK_INAND_CMD38),
  1932. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1933. MMC_QUIRK_INAND_CMD38),
  1934. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1935. MMC_QUIRK_INAND_CMD38),
  1936. MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1937. MMC_QUIRK_INAND_CMD38),
  1938. /*
  1939. * Some MMC cards experience performance degradation with CMD23
  1940. * instead of CMD12-bounded multiblock transfers. For now we'll
  1941. * black list what's bad...
  1942. * - Certain Toshiba cards.
  1943. *
  1944. * N.B. This doesn't affect SD cards.
  1945. */
  1946. MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1947. MMC_QUIRK_BLK_NO_CMD23),
  1948. MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1949. MMC_QUIRK_BLK_NO_CMD23),
  1950. MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1951. MMC_QUIRK_BLK_NO_CMD23),
  1952. /*
  1953. * Some Micron MMC cards needs longer data read timeout than
  1954. * indicated in CSD.
  1955. */
  1956. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
  1957. MMC_QUIRK_LONG_READ_TIME),
  1958. /*
  1959. * On these Samsung MoviNAND parts, performing secure erase or
  1960. * secure trim can result in unrecoverable corruption due to a
  1961. * firmware bug.
  1962. */
  1963. MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1964. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1965. MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1966. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1967. MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1968. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1969. MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1970. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1971. MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1972. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1973. MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1974. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1975. MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1976. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1977. MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1978. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1979. END_FIXUP
  1980. };
  1981. static int mmc_blk_probe(struct mmc_card *card)
  1982. {
  1983. struct mmc_blk_data *md, *part_md;
  1984. char cap_str[10];
  1985. /*
  1986. * Check that the card supports the command class(es) we need.
  1987. */
  1988. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1989. return -ENODEV;
  1990. md = mmc_blk_alloc(card);
  1991. if (IS_ERR(md))
  1992. return PTR_ERR(md);
  1993. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1994. cap_str, sizeof(cap_str));
  1995. pr_info("%s: %s %s %s %s\n",
  1996. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1997. cap_str, md->read_only ? "(ro)" : "");
  1998. if (mmc_blk_alloc_parts(card, md))
  1999. goto out;
  2000. mmc_set_drvdata(card, md);
  2001. mmc_fixup_device(card, blk_fixups);
  2002. if (mmc_add_disk(md))
  2003. goto out;
  2004. list_for_each_entry(part_md, &md->part, part) {
  2005. if (mmc_add_disk(part_md))
  2006. goto out;
  2007. }
  2008. return 0;
  2009. out:
  2010. mmc_blk_remove_parts(card, md);
  2011. mmc_blk_remove_req(md);
  2012. return 0;
  2013. }
  2014. static void mmc_blk_remove(struct mmc_card *card)
  2015. {
  2016. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2017. mmc_blk_remove_parts(card, md);
  2018. mmc_claim_host(card->host);
  2019. mmc_blk_part_switch(card, md);
  2020. mmc_release_host(card->host);
  2021. mmc_blk_remove_req(md);
  2022. mmc_set_drvdata(card, NULL);
  2023. }
  2024. #ifdef CONFIG_PM
  2025. static int mmc_blk_suspend(struct mmc_card *card)
  2026. {
  2027. struct mmc_blk_data *part_md;
  2028. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2029. if (md) {
  2030. mmc_queue_suspend(&md->queue);
  2031. list_for_each_entry(part_md, &md->part, part) {
  2032. mmc_queue_suspend(&part_md->queue);
  2033. }
  2034. }
  2035. return 0;
  2036. }
  2037. static int mmc_blk_resume(struct mmc_card *card)
  2038. {
  2039. struct mmc_blk_data *part_md;
  2040. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2041. if (md) {
  2042. /*
  2043. * Resume involves the card going into idle state,
  2044. * so current partition is always the main one.
  2045. */
  2046. md->part_curr = md->part_type;
  2047. mmc_queue_resume(&md->queue);
  2048. list_for_each_entry(part_md, &md->part, part) {
  2049. mmc_queue_resume(&part_md->queue);
  2050. }
  2051. }
  2052. return 0;
  2053. }
  2054. #else
  2055. #define mmc_blk_suspend NULL
  2056. #define mmc_blk_resume NULL
  2057. #endif
  2058. static struct mmc_driver mmc_driver = {
  2059. .drv = {
  2060. .name = "mmcblk",
  2061. },
  2062. .probe = mmc_blk_probe,
  2063. .remove = mmc_blk_remove,
  2064. .suspend = mmc_blk_suspend,
  2065. .resume = mmc_blk_resume,
  2066. };
  2067. static int __init mmc_blk_init(void)
  2068. {
  2069. int res;
  2070. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2071. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2072. max_devices = 256 / perdev_minors;
  2073. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2074. if (res)
  2075. goto out;
  2076. res = mmc_register_driver(&mmc_driver);
  2077. if (res)
  2078. goto out2;
  2079. return 0;
  2080. out2:
  2081. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2082. out:
  2083. return res;
  2084. }
  2085. static void __exit mmc_blk_exit(void)
  2086. {
  2087. mmc_unregister_driver(&mmc_driver);
  2088. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2089. }
  2090. module_init(mmc_blk_init);
  2091. module_exit(mmc_blk_exit);
  2092. MODULE_LICENSE("GPL");
  2093. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");