block.c 58 KB

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