block.c 56 KB

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