block.c 68 KB

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