block.c 71 KB

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