block.c 64 KB

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