block.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599
  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. int mmc_access_rpmb(struct mmc_queue *mq)
  855. {
  856. struct mmc_blk_data *md = mq->data;
  857. /*
  858. * If this is a RPMB partition access, return ture
  859. */
  860. if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
  861. return true;
  862. return false;
  863. }
  864. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  865. {
  866. struct mmc_blk_data *md = mq->data;
  867. struct mmc_card *card = md->queue.card;
  868. unsigned int from, nr, arg;
  869. int err = 0, type = MMC_BLK_DISCARD;
  870. if (!mmc_can_erase(card)) {
  871. err = -EOPNOTSUPP;
  872. goto out;
  873. }
  874. from = blk_rq_pos(req);
  875. nr = blk_rq_sectors(req);
  876. if (mmc_can_discard(card))
  877. arg = MMC_DISCARD_ARG;
  878. else if (mmc_can_trim(card))
  879. arg = MMC_TRIM_ARG;
  880. else
  881. arg = MMC_ERASE_ARG;
  882. retry:
  883. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  884. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  885. INAND_CMD38_ARG_EXT_CSD,
  886. arg == MMC_TRIM_ARG ?
  887. INAND_CMD38_ARG_TRIM :
  888. INAND_CMD38_ARG_ERASE,
  889. 0);
  890. if (err)
  891. goto out;
  892. }
  893. err = mmc_erase(card, from, nr, arg);
  894. out:
  895. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  896. goto retry;
  897. if (!err)
  898. mmc_blk_reset_success(md, type);
  899. blk_end_request(req, err, blk_rq_bytes(req));
  900. return err ? 0 : 1;
  901. }
  902. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  903. struct request *req)
  904. {
  905. struct mmc_blk_data *md = mq->data;
  906. struct mmc_card *card = md->queue.card;
  907. unsigned int from, nr, arg;
  908. int err = 0, type = MMC_BLK_SECDISCARD;
  909. if (!(mmc_can_secure_erase_trim(card))) {
  910. err = -EOPNOTSUPP;
  911. goto out;
  912. }
  913. from = blk_rq_pos(req);
  914. nr = blk_rq_sectors(req);
  915. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  916. arg = MMC_SECURE_TRIM1_ARG;
  917. else
  918. arg = MMC_SECURE_ERASE_ARG;
  919. retry:
  920. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  921. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  922. INAND_CMD38_ARG_EXT_CSD,
  923. arg == MMC_SECURE_TRIM1_ARG ?
  924. INAND_CMD38_ARG_SECTRIM1 :
  925. INAND_CMD38_ARG_SECERASE,
  926. 0);
  927. if (err)
  928. goto out_retry;
  929. }
  930. err = mmc_erase(card, from, nr, arg);
  931. if (err == -EIO)
  932. goto out_retry;
  933. if (err)
  934. goto out;
  935. if (arg == MMC_SECURE_TRIM1_ARG) {
  936. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  937. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  938. INAND_CMD38_ARG_EXT_CSD,
  939. INAND_CMD38_ARG_SECTRIM2,
  940. 0);
  941. if (err)
  942. goto out_retry;
  943. }
  944. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  945. if (err == -EIO)
  946. goto out_retry;
  947. if (err)
  948. goto out;
  949. }
  950. out_retry:
  951. if (err && !mmc_blk_reset(md, card->host, type))
  952. goto retry;
  953. if (!err)
  954. mmc_blk_reset_success(md, type);
  955. out:
  956. blk_end_request(req, err, blk_rq_bytes(req));
  957. return err ? 0 : 1;
  958. }
  959. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  960. {
  961. struct mmc_blk_data *md = mq->data;
  962. struct mmc_card *card = md->queue.card;
  963. int ret = 0;
  964. ret = mmc_flush_cache(card);
  965. if (ret)
  966. ret = -EIO;
  967. blk_end_request_all(req, ret);
  968. return ret ? 0 : 1;
  969. }
  970. /*
  971. * Reformat current write as a reliable write, supporting
  972. * both legacy and the enhanced reliable write MMC cards.
  973. * In each transfer we'll handle only as much as a single
  974. * reliable write can handle, thus finish the request in
  975. * partial completions.
  976. */
  977. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  978. struct mmc_card *card,
  979. struct request *req)
  980. {
  981. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  982. /* Legacy mode imposes restrictions on transfers. */
  983. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  984. brq->data.blocks = 1;
  985. if (brq->data.blocks > card->ext_csd.rel_sectors)
  986. brq->data.blocks = card->ext_csd.rel_sectors;
  987. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  988. brq->data.blocks = 1;
  989. }
  990. }
  991. #define CMD_ERRORS \
  992. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  993. R1_ADDRESS_ERROR | /* Misaligned address */ \
  994. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  995. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  996. R1_CC_ERROR | /* Card controller error */ \
  997. R1_ERROR) /* General/unknown error */
  998. static int mmc_blk_err_check(struct mmc_card *card,
  999. struct mmc_async_req *areq)
  1000. {
  1001. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  1002. mmc_active);
  1003. struct mmc_blk_request *brq = &mq_mrq->brq;
  1004. struct request *req = mq_mrq->req;
  1005. int ecc_err = 0, gen_err = 0;
  1006. /*
  1007. * sbc.error indicates a problem with the set block count
  1008. * command. No data will have been transferred.
  1009. *
  1010. * cmd.error indicates a problem with the r/w command. No
  1011. * data will have been transferred.
  1012. *
  1013. * stop.error indicates a problem with the stop command. Data
  1014. * may have been transferred, or may still be transferring.
  1015. */
  1016. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  1017. brq->data.error) {
  1018. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
  1019. case ERR_RETRY:
  1020. return MMC_BLK_RETRY;
  1021. case ERR_ABORT:
  1022. return MMC_BLK_ABORT;
  1023. case ERR_NOMEDIUM:
  1024. return MMC_BLK_NOMEDIUM;
  1025. case ERR_CONTINUE:
  1026. break;
  1027. }
  1028. }
  1029. /*
  1030. * Check for errors relating to the execution of the
  1031. * initial command - such as address errors. No data
  1032. * has been transferred.
  1033. */
  1034. if (brq->cmd.resp[0] & CMD_ERRORS) {
  1035. pr_err("%s: r/w command failed, status = %#x\n",
  1036. req->rq_disk->disk_name, brq->cmd.resp[0]);
  1037. return MMC_BLK_ABORT;
  1038. }
  1039. /*
  1040. * Everything else is either success, or a data error of some
  1041. * kind. If it was a write, we may have transitioned to
  1042. * program mode, which we have to wait for it to complete.
  1043. */
  1044. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  1045. int err;
  1046. /* Check stop command response */
  1047. if (brq->stop.resp[0] & R1_ERROR) {
  1048. pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
  1049. req->rq_disk->disk_name, __func__,
  1050. brq->stop.resp[0]);
  1051. gen_err = 1;
  1052. }
  1053. err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
  1054. &gen_err);
  1055. if (err)
  1056. return MMC_BLK_CMD_ERR;
  1057. }
  1058. /* if general error occurs, retry the write operation. */
  1059. if (gen_err) {
  1060. pr_warn("%s: retrying write for general error\n",
  1061. req->rq_disk->disk_name);
  1062. return MMC_BLK_RETRY;
  1063. }
  1064. if (brq->data.error) {
  1065. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  1066. req->rq_disk->disk_name, brq->data.error,
  1067. (unsigned)blk_rq_pos(req),
  1068. (unsigned)blk_rq_sectors(req),
  1069. brq->cmd.resp[0], brq->stop.resp[0]);
  1070. if (rq_data_dir(req) == READ) {
  1071. if (ecc_err)
  1072. return MMC_BLK_ECC_ERR;
  1073. return MMC_BLK_DATA_ERR;
  1074. } else {
  1075. return MMC_BLK_CMD_ERR;
  1076. }
  1077. }
  1078. if (!brq->data.bytes_xfered)
  1079. return MMC_BLK_RETRY;
  1080. if (mmc_packed_cmd(mq_mrq->cmd_type)) {
  1081. if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
  1082. return MMC_BLK_PARTIAL;
  1083. else
  1084. return MMC_BLK_SUCCESS;
  1085. }
  1086. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1087. return MMC_BLK_PARTIAL;
  1088. return MMC_BLK_SUCCESS;
  1089. }
  1090. static int mmc_blk_packed_err_check(struct mmc_card *card,
  1091. struct mmc_async_req *areq)
  1092. {
  1093. struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
  1094. mmc_active);
  1095. struct request *req = mq_rq->req;
  1096. struct mmc_packed *packed = mq_rq->packed;
  1097. int err, check, status;
  1098. u8 *ext_csd;
  1099. BUG_ON(!packed);
  1100. packed->retries--;
  1101. check = mmc_blk_err_check(card, areq);
  1102. err = get_card_status(card, &status, 0);
  1103. if (err) {
  1104. pr_err("%s: error %d sending status command\n",
  1105. req->rq_disk->disk_name, err);
  1106. return MMC_BLK_ABORT;
  1107. }
  1108. if (status & R1_EXCEPTION_EVENT) {
  1109. err = mmc_get_ext_csd(card, &ext_csd);
  1110. if (err) {
  1111. pr_err("%s: error %d sending ext_csd\n",
  1112. req->rq_disk->disk_name, err);
  1113. return MMC_BLK_ABORT;
  1114. }
  1115. if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
  1116. EXT_CSD_PACKED_FAILURE) &&
  1117. (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1118. EXT_CSD_PACKED_GENERIC_ERROR)) {
  1119. if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1120. EXT_CSD_PACKED_INDEXED_ERROR) {
  1121. packed->idx_failure =
  1122. ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
  1123. check = MMC_BLK_PARTIAL;
  1124. }
  1125. pr_err("%s: packed cmd failed, nr %u, sectors %u, "
  1126. "failure index: %d\n",
  1127. req->rq_disk->disk_name, packed->nr_entries,
  1128. packed->blocks, packed->idx_failure);
  1129. }
  1130. kfree(ext_csd);
  1131. }
  1132. return check;
  1133. }
  1134. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1135. struct mmc_card *card,
  1136. int disable_multi,
  1137. struct mmc_queue *mq)
  1138. {
  1139. u32 readcmd, writecmd;
  1140. struct mmc_blk_request *brq = &mqrq->brq;
  1141. struct request *req = mqrq->req;
  1142. struct mmc_blk_data *md = mq->data;
  1143. bool do_data_tag;
  1144. /*
  1145. * Reliable writes are used to implement Forced Unit Access and
  1146. * REQ_META accesses, and are supported only on MMCs.
  1147. *
  1148. * XXX: this really needs a good explanation of why REQ_META
  1149. * is treated special.
  1150. */
  1151. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  1152. (req->cmd_flags & REQ_META)) &&
  1153. (rq_data_dir(req) == WRITE) &&
  1154. (md->flags & MMC_BLK_REL_WR);
  1155. memset(brq, 0, sizeof(struct mmc_blk_request));
  1156. brq->mrq.cmd = &brq->cmd;
  1157. brq->mrq.data = &brq->data;
  1158. brq->cmd.arg = blk_rq_pos(req);
  1159. if (!mmc_card_blockaddr(card))
  1160. brq->cmd.arg <<= 9;
  1161. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1162. brq->data.blksz = 512;
  1163. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1164. brq->stop.arg = 0;
  1165. brq->data.blocks = blk_rq_sectors(req);
  1166. /*
  1167. * The block layer doesn't support all sector count
  1168. * restrictions, so we need to be prepared for too big
  1169. * requests.
  1170. */
  1171. if (brq->data.blocks > card->host->max_blk_count)
  1172. brq->data.blocks = card->host->max_blk_count;
  1173. if (brq->data.blocks > 1) {
  1174. /*
  1175. * After a read error, we redo the request one sector
  1176. * at a time in order to accurately determine which
  1177. * sectors can be read successfully.
  1178. */
  1179. if (disable_multi)
  1180. brq->data.blocks = 1;
  1181. /*
  1182. * Some controllers have HW issues while operating
  1183. * in multiple I/O mode
  1184. */
  1185. if (card->host->ops->multi_io_quirk)
  1186. brq->data.blocks = card->host->ops->multi_io_quirk(card,
  1187. (rq_data_dir(req) == READ) ?
  1188. MMC_DATA_READ : MMC_DATA_WRITE,
  1189. brq->data.blocks);
  1190. }
  1191. if (brq->data.blocks > 1 || do_rel_wr) {
  1192. /* SPI multiblock writes terminate using a special
  1193. * token, not a STOP_TRANSMISSION request.
  1194. */
  1195. if (!mmc_host_is_spi(card->host) ||
  1196. rq_data_dir(req) == READ)
  1197. brq->mrq.stop = &brq->stop;
  1198. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1199. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1200. } else {
  1201. brq->mrq.stop = NULL;
  1202. readcmd = MMC_READ_SINGLE_BLOCK;
  1203. writecmd = MMC_WRITE_BLOCK;
  1204. }
  1205. if (rq_data_dir(req) == READ) {
  1206. brq->cmd.opcode = readcmd;
  1207. brq->data.flags |= MMC_DATA_READ;
  1208. if (brq->mrq.stop)
  1209. brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
  1210. MMC_CMD_AC;
  1211. } else {
  1212. brq->cmd.opcode = writecmd;
  1213. brq->data.flags |= MMC_DATA_WRITE;
  1214. if (brq->mrq.stop)
  1215. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
  1216. MMC_CMD_AC;
  1217. }
  1218. if (do_rel_wr)
  1219. mmc_apply_rel_rw(brq, card, req);
  1220. /*
  1221. * Data tag is used only during writing meta data to speed
  1222. * up write and any subsequent read of this meta data
  1223. */
  1224. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1225. (req->cmd_flags & REQ_META) &&
  1226. (rq_data_dir(req) == WRITE) &&
  1227. ((brq->data.blocks * brq->data.blksz) >=
  1228. card->ext_csd.data_tag_unit_size);
  1229. /*
  1230. * Pre-defined multi-block transfers are preferable to
  1231. * open ended-ones (and necessary for reliable writes).
  1232. * However, it is not sufficient to just send CMD23,
  1233. * and avoid the final CMD12, as on an error condition
  1234. * CMD12 (stop) needs to be sent anyway. This, coupled
  1235. * with Auto-CMD23 enhancements provided by some
  1236. * hosts, means that the complexity of dealing
  1237. * with this is best left to the host. If CMD23 is
  1238. * supported by card and host, we'll fill sbc in and let
  1239. * the host deal with handling it correctly. This means
  1240. * that for hosts that don't expose MMC_CAP_CMD23, no
  1241. * change of behavior will be observed.
  1242. *
  1243. * N.B: Some MMC cards experience perf degradation.
  1244. * We'll avoid using CMD23-bounded multiblock writes for
  1245. * these, while retaining features like reliable writes.
  1246. */
  1247. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1248. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1249. do_data_tag)) {
  1250. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1251. brq->sbc.arg = brq->data.blocks |
  1252. (do_rel_wr ? (1 << 31) : 0) |
  1253. (do_data_tag ? (1 << 29) : 0);
  1254. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1255. brq->mrq.sbc = &brq->sbc;
  1256. }
  1257. mmc_set_data_timeout(&brq->data, card);
  1258. brq->data.sg = mqrq->sg;
  1259. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1260. /*
  1261. * Adjust the sg list so it is the same size as the
  1262. * request.
  1263. */
  1264. if (brq->data.blocks != blk_rq_sectors(req)) {
  1265. int i, data_size = brq->data.blocks << 9;
  1266. struct scatterlist *sg;
  1267. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1268. data_size -= sg->length;
  1269. if (data_size <= 0) {
  1270. sg->length += data_size;
  1271. i++;
  1272. break;
  1273. }
  1274. }
  1275. brq->data.sg_len = i;
  1276. }
  1277. mqrq->mmc_active.mrq = &brq->mrq;
  1278. mqrq->mmc_active.err_check = mmc_blk_err_check;
  1279. mmc_queue_bounce_pre(mqrq);
  1280. }
  1281. static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
  1282. struct mmc_card *card)
  1283. {
  1284. unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
  1285. unsigned int max_seg_sz = queue_max_segment_size(q);
  1286. unsigned int len, nr_segs = 0;
  1287. do {
  1288. len = min(hdr_sz, max_seg_sz);
  1289. hdr_sz -= len;
  1290. nr_segs++;
  1291. } while (hdr_sz);
  1292. return nr_segs;
  1293. }
  1294. static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
  1295. {
  1296. struct request_queue *q = mq->queue;
  1297. struct mmc_card *card = mq->card;
  1298. struct request *cur = req, *next = NULL;
  1299. struct mmc_blk_data *md = mq->data;
  1300. struct mmc_queue_req *mqrq = mq->mqrq_cur;
  1301. bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
  1302. unsigned int req_sectors = 0, phys_segments = 0;
  1303. unsigned int max_blk_count, max_phys_segs;
  1304. bool put_back = true;
  1305. u8 max_packed_rw = 0;
  1306. u8 reqs = 0;
  1307. if (!(md->flags & MMC_BLK_PACKED_CMD))
  1308. goto no_packed;
  1309. if ((rq_data_dir(cur) == WRITE) &&
  1310. mmc_host_packed_wr(card->host))
  1311. max_packed_rw = card->ext_csd.max_packed_writes;
  1312. if (max_packed_rw == 0)
  1313. goto no_packed;
  1314. if (mmc_req_rel_wr(cur) &&
  1315. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1316. goto no_packed;
  1317. if (mmc_large_sector(card) &&
  1318. !IS_ALIGNED(blk_rq_sectors(cur), 8))
  1319. goto no_packed;
  1320. mmc_blk_clear_packed(mqrq);
  1321. max_blk_count = min(card->host->max_blk_count,
  1322. card->host->max_req_size >> 9);
  1323. if (unlikely(max_blk_count > 0xffff))
  1324. max_blk_count = 0xffff;
  1325. max_phys_segs = queue_max_segments(q);
  1326. req_sectors += blk_rq_sectors(cur);
  1327. phys_segments += cur->nr_phys_segments;
  1328. if (rq_data_dir(cur) == WRITE) {
  1329. req_sectors += mmc_large_sector(card) ? 8 : 1;
  1330. phys_segments += mmc_calc_packed_hdr_segs(q, card);
  1331. }
  1332. do {
  1333. if (reqs >= max_packed_rw - 1) {
  1334. put_back = false;
  1335. break;
  1336. }
  1337. spin_lock_irq(q->queue_lock);
  1338. next = blk_fetch_request(q);
  1339. spin_unlock_irq(q->queue_lock);
  1340. if (!next) {
  1341. put_back = false;
  1342. break;
  1343. }
  1344. if (mmc_large_sector(card) &&
  1345. !IS_ALIGNED(blk_rq_sectors(next), 8))
  1346. break;
  1347. if (next->cmd_flags & REQ_DISCARD ||
  1348. next->cmd_flags & REQ_FLUSH)
  1349. break;
  1350. if (rq_data_dir(cur) != rq_data_dir(next))
  1351. break;
  1352. if (mmc_req_rel_wr(next) &&
  1353. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1354. break;
  1355. req_sectors += blk_rq_sectors(next);
  1356. if (req_sectors > max_blk_count)
  1357. break;
  1358. phys_segments += next->nr_phys_segments;
  1359. if (phys_segments > max_phys_segs)
  1360. break;
  1361. list_add_tail(&next->queuelist, &mqrq->packed->list);
  1362. cur = next;
  1363. reqs++;
  1364. } while (1);
  1365. if (put_back) {
  1366. spin_lock_irq(q->queue_lock);
  1367. blk_requeue_request(q, next);
  1368. spin_unlock_irq(q->queue_lock);
  1369. }
  1370. if (reqs > 0) {
  1371. list_add(&req->queuelist, &mqrq->packed->list);
  1372. mqrq->packed->nr_entries = ++reqs;
  1373. mqrq->packed->retries = reqs;
  1374. return reqs;
  1375. }
  1376. no_packed:
  1377. mqrq->cmd_type = MMC_PACKED_NONE;
  1378. return 0;
  1379. }
  1380. static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
  1381. struct mmc_card *card,
  1382. struct mmc_queue *mq)
  1383. {
  1384. struct mmc_blk_request *brq = &mqrq->brq;
  1385. struct request *req = mqrq->req;
  1386. struct request *prq;
  1387. struct mmc_blk_data *md = mq->data;
  1388. struct mmc_packed *packed = mqrq->packed;
  1389. bool do_rel_wr, do_data_tag;
  1390. u32 *packed_cmd_hdr;
  1391. u8 hdr_blocks;
  1392. u8 i = 1;
  1393. BUG_ON(!packed);
  1394. mqrq->cmd_type = MMC_PACKED_WRITE;
  1395. packed->blocks = 0;
  1396. packed->idx_failure = MMC_PACKED_NR_IDX;
  1397. packed_cmd_hdr = packed->cmd_hdr;
  1398. memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
  1399. packed_cmd_hdr[0] = (packed->nr_entries << 16) |
  1400. (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
  1401. hdr_blocks = mmc_large_sector(card) ? 8 : 1;
  1402. /*
  1403. * Argument for each entry of packed group
  1404. */
  1405. list_for_each_entry(prq, &packed->list, queuelist) {
  1406. do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
  1407. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1408. (prq->cmd_flags & REQ_META) &&
  1409. (rq_data_dir(prq) == WRITE) &&
  1410. ((brq->data.blocks * brq->data.blksz) >=
  1411. card->ext_csd.data_tag_unit_size);
  1412. /* Argument of CMD23 */
  1413. packed_cmd_hdr[(i * 2)] =
  1414. (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
  1415. (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
  1416. blk_rq_sectors(prq);
  1417. /* Argument of CMD18 or CMD25 */
  1418. packed_cmd_hdr[((i * 2)) + 1] =
  1419. mmc_card_blockaddr(card) ?
  1420. blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
  1421. packed->blocks += blk_rq_sectors(prq);
  1422. i++;
  1423. }
  1424. memset(brq, 0, sizeof(struct mmc_blk_request));
  1425. brq->mrq.cmd = &brq->cmd;
  1426. brq->mrq.data = &brq->data;
  1427. brq->mrq.sbc = &brq->sbc;
  1428. brq->mrq.stop = &brq->stop;
  1429. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1430. brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
  1431. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1432. brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
  1433. brq->cmd.arg = blk_rq_pos(req);
  1434. if (!mmc_card_blockaddr(card))
  1435. brq->cmd.arg <<= 9;
  1436. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1437. brq->data.blksz = 512;
  1438. brq->data.blocks = packed->blocks + hdr_blocks;
  1439. brq->data.flags |= MMC_DATA_WRITE;
  1440. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1441. brq->stop.arg = 0;
  1442. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1443. mmc_set_data_timeout(&brq->data, card);
  1444. brq->data.sg = mqrq->sg;
  1445. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1446. mqrq->mmc_active.mrq = &brq->mrq;
  1447. mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
  1448. mmc_queue_bounce_pre(mqrq);
  1449. }
  1450. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  1451. struct mmc_blk_request *brq, struct request *req,
  1452. int ret)
  1453. {
  1454. struct mmc_queue_req *mq_rq;
  1455. mq_rq = container_of(brq, struct mmc_queue_req, brq);
  1456. /*
  1457. * If this is an SD card and we're writing, we can first
  1458. * mark the known good sectors as ok.
  1459. *
  1460. * If the card is not SD, we can still ok written sectors
  1461. * as reported by the controller (which might be less than
  1462. * the real number of written sectors, but never more).
  1463. */
  1464. if (mmc_card_sd(card)) {
  1465. u32 blocks;
  1466. blocks = mmc_sd_num_wr_blocks(card);
  1467. if (blocks != (u32)-1) {
  1468. ret = blk_end_request(req, 0, blocks << 9);
  1469. }
  1470. } else {
  1471. if (!mmc_packed_cmd(mq_rq->cmd_type))
  1472. ret = blk_end_request(req, 0, brq->data.bytes_xfered);
  1473. }
  1474. return ret;
  1475. }
  1476. static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
  1477. {
  1478. struct request *prq;
  1479. struct mmc_packed *packed = mq_rq->packed;
  1480. int idx = packed->idx_failure, i = 0;
  1481. int ret = 0;
  1482. BUG_ON(!packed);
  1483. while (!list_empty(&packed->list)) {
  1484. prq = list_entry_rq(packed->list.next);
  1485. if (idx == i) {
  1486. /* retry from error index */
  1487. packed->nr_entries -= idx;
  1488. mq_rq->req = prq;
  1489. ret = 1;
  1490. if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
  1491. list_del_init(&prq->queuelist);
  1492. mmc_blk_clear_packed(mq_rq);
  1493. }
  1494. return ret;
  1495. }
  1496. list_del_init(&prq->queuelist);
  1497. blk_end_request(prq, 0, blk_rq_bytes(prq));
  1498. i++;
  1499. }
  1500. mmc_blk_clear_packed(mq_rq);
  1501. return ret;
  1502. }
  1503. static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
  1504. {
  1505. struct request *prq;
  1506. struct mmc_packed *packed = mq_rq->packed;
  1507. BUG_ON(!packed);
  1508. while (!list_empty(&packed->list)) {
  1509. prq = list_entry_rq(packed->list.next);
  1510. list_del_init(&prq->queuelist);
  1511. blk_end_request(prq, -EIO, blk_rq_bytes(prq));
  1512. }
  1513. mmc_blk_clear_packed(mq_rq);
  1514. }
  1515. static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
  1516. struct mmc_queue_req *mq_rq)
  1517. {
  1518. struct request *prq;
  1519. struct request_queue *q = mq->queue;
  1520. struct mmc_packed *packed = mq_rq->packed;
  1521. BUG_ON(!packed);
  1522. while (!list_empty(&packed->list)) {
  1523. prq = list_entry_rq(packed->list.prev);
  1524. if (prq->queuelist.prev != &packed->list) {
  1525. list_del_init(&prq->queuelist);
  1526. spin_lock_irq(q->queue_lock);
  1527. blk_requeue_request(mq->queue, prq);
  1528. spin_unlock_irq(q->queue_lock);
  1529. } else {
  1530. list_del_init(&prq->queuelist);
  1531. }
  1532. }
  1533. mmc_blk_clear_packed(mq_rq);
  1534. }
  1535. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  1536. {
  1537. struct mmc_blk_data *md = mq->data;
  1538. struct mmc_card *card = md->queue.card;
  1539. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  1540. int ret = 1, disable_multi = 0, retry = 0, type;
  1541. enum mmc_blk_status status;
  1542. struct mmc_queue_req *mq_rq;
  1543. struct request *req = rqc;
  1544. struct mmc_async_req *areq;
  1545. const u8 packed_nr = 2;
  1546. u8 reqs = 0;
  1547. if (!rqc && !mq->mqrq_prev->req)
  1548. return 0;
  1549. if (rqc)
  1550. reqs = mmc_blk_prep_packed_list(mq, rqc);
  1551. do {
  1552. if (rqc) {
  1553. /*
  1554. * When 4KB native sector is enabled, only 8 blocks
  1555. * multiple read or write is allowed
  1556. */
  1557. if ((brq->data.blocks & 0x07) &&
  1558. (card->ext_csd.data_sector_size == 4096)) {
  1559. pr_err("%s: Transfer size is not 4KB sector size aligned\n",
  1560. req->rq_disk->disk_name);
  1561. mq_rq = mq->mqrq_cur;
  1562. goto cmd_abort;
  1563. }
  1564. if (reqs >= packed_nr)
  1565. mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
  1566. card, mq);
  1567. else
  1568. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1569. areq = &mq->mqrq_cur->mmc_active;
  1570. } else
  1571. areq = NULL;
  1572. areq = mmc_start_req(card->host, areq, (int *) &status);
  1573. if (!areq) {
  1574. if (status == MMC_BLK_NEW_REQUEST)
  1575. mq->flags |= MMC_QUEUE_NEW_REQUEST;
  1576. return 0;
  1577. }
  1578. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  1579. brq = &mq_rq->brq;
  1580. req = mq_rq->req;
  1581. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1582. mmc_queue_bounce_post(mq_rq);
  1583. switch (status) {
  1584. case MMC_BLK_SUCCESS:
  1585. case MMC_BLK_PARTIAL:
  1586. /*
  1587. * A block was successfully transferred.
  1588. */
  1589. mmc_blk_reset_success(md, type);
  1590. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1591. ret = mmc_blk_end_packed_req(mq_rq);
  1592. break;
  1593. } else {
  1594. ret = blk_end_request(req, 0,
  1595. brq->data.bytes_xfered);
  1596. }
  1597. /*
  1598. * If the blk_end_request function returns non-zero even
  1599. * though all data has been transferred and no errors
  1600. * were returned by the host controller, it's a bug.
  1601. */
  1602. if (status == MMC_BLK_SUCCESS && ret) {
  1603. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1604. __func__, blk_rq_bytes(req),
  1605. brq->data.bytes_xfered);
  1606. rqc = NULL;
  1607. goto cmd_abort;
  1608. }
  1609. break;
  1610. case MMC_BLK_CMD_ERR:
  1611. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1612. if (!mmc_blk_reset(md, card->host, type))
  1613. break;
  1614. goto cmd_abort;
  1615. case MMC_BLK_RETRY:
  1616. if (retry++ < 5)
  1617. break;
  1618. /* Fall through */
  1619. case MMC_BLK_ABORT:
  1620. if (!mmc_blk_reset(md, card->host, type))
  1621. break;
  1622. goto cmd_abort;
  1623. case MMC_BLK_DATA_ERR: {
  1624. int err;
  1625. err = mmc_blk_reset(md, card->host, type);
  1626. if (!err)
  1627. break;
  1628. if (err == -ENODEV ||
  1629. mmc_packed_cmd(mq_rq->cmd_type))
  1630. goto cmd_abort;
  1631. /* Fall through */
  1632. }
  1633. case MMC_BLK_ECC_ERR:
  1634. if (brq->data.blocks > 1) {
  1635. /* Redo read one sector at a time */
  1636. pr_warn("%s: retrying using single block read\n",
  1637. req->rq_disk->disk_name);
  1638. disable_multi = 1;
  1639. break;
  1640. }
  1641. /*
  1642. * After an error, we redo I/O one sector at a
  1643. * time, so we only reach here after trying to
  1644. * read a single sector.
  1645. */
  1646. ret = blk_end_request(req, -EIO,
  1647. brq->data.blksz);
  1648. if (!ret)
  1649. goto start_new_req;
  1650. break;
  1651. case MMC_BLK_NOMEDIUM:
  1652. goto cmd_abort;
  1653. default:
  1654. pr_err("%s: Unhandled return value (%d)",
  1655. req->rq_disk->disk_name, status);
  1656. goto cmd_abort;
  1657. }
  1658. if (ret) {
  1659. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1660. if (!mq_rq->packed->retries)
  1661. goto cmd_abort;
  1662. mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
  1663. mmc_start_req(card->host,
  1664. &mq_rq->mmc_active, NULL);
  1665. } else {
  1666. /*
  1667. * In case of a incomplete request
  1668. * prepare it again and resend.
  1669. */
  1670. mmc_blk_rw_rq_prep(mq_rq, card,
  1671. disable_multi, mq);
  1672. mmc_start_req(card->host,
  1673. &mq_rq->mmc_active, NULL);
  1674. }
  1675. }
  1676. } while (ret);
  1677. return 1;
  1678. cmd_abort:
  1679. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1680. mmc_blk_abort_packed_req(mq_rq);
  1681. } else {
  1682. if (mmc_card_removed(card))
  1683. req->cmd_flags |= REQ_QUIET;
  1684. while (ret)
  1685. ret = blk_end_request(req, -EIO,
  1686. blk_rq_cur_bytes(req));
  1687. }
  1688. start_new_req:
  1689. if (rqc) {
  1690. if (mmc_card_removed(card)) {
  1691. rqc->cmd_flags |= REQ_QUIET;
  1692. blk_end_request_all(rqc, -EIO);
  1693. } else {
  1694. /*
  1695. * If current request is packed, it needs to put back.
  1696. */
  1697. if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
  1698. mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
  1699. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1700. mmc_start_req(card->host,
  1701. &mq->mqrq_cur->mmc_active, NULL);
  1702. }
  1703. }
  1704. return 0;
  1705. }
  1706. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1707. {
  1708. int ret;
  1709. struct mmc_blk_data *md = mq->data;
  1710. struct mmc_card *card = md->queue.card;
  1711. struct mmc_host *host = card->host;
  1712. unsigned long flags;
  1713. unsigned int cmd_flags = req ? req->cmd_flags : 0;
  1714. if (req && !mq->mqrq_prev->req)
  1715. /* claim host only for the first request */
  1716. mmc_get_card(card);
  1717. ret = mmc_blk_part_switch(card, md);
  1718. if (ret) {
  1719. if (req) {
  1720. blk_end_request_all(req, -EIO);
  1721. }
  1722. ret = 0;
  1723. goto out;
  1724. }
  1725. mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
  1726. if (cmd_flags & REQ_DISCARD) {
  1727. /* complete ongoing async transfer before issuing discard */
  1728. if (card->host->areq)
  1729. mmc_blk_issue_rw_rq(mq, NULL);
  1730. if (req->cmd_flags & REQ_SECURE)
  1731. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1732. else
  1733. ret = mmc_blk_issue_discard_rq(mq, req);
  1734. } else if (cmd_flags & REQ_FLUSH) {
  1735. /* complete ongoing async transfer before issuing flush */
  1736. if (card->host->areq)
  1737. mmc_blk_issue_rw_rq(mq, NULL);
  1738. ret = mmc_blk_issue_flush(mq, req);
  1739. } else {
  1740. if (!req && host->areq) {
  1741. spin_lock_irqsave(&host->context_info.lock, flags);
  1742. host->context_info.is_waiting_last_req = true;
  1743. spin_unlock_irqrestore(&host->context_info.lock, flags);
  1744. }
  1745. ret = mmc_blk_issue_rw_rq(mq, req);
  1746. }
  1747. out:
  1748. if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
  1749. (cmd_flags & MMC_REQ_SPECIAL_MASK))
  1750. /*
  1751. * Release host when there are no more requests
  1752. * and after special request(discard, flush) is done.
  1753. * In case sepecial request, there is no reentry to
  1754. * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
  1755. */
  1756. mmc_put_card(card);
  1757. return ret;
  1758. }
  1759. static inline int mmc_blk_readonly(struct mmc_card *card)
  1760. {
  1761. return mmc_card_readonly(card) ||
  1762. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1763. }
  1764. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1765. struct device *parent,
  1766. sector_t size,
  1767. bool default_ro,
  1768. const char *subname,
  1769. int area_type)
  1770. {
  1771. struct mmc_blk_data *md;
  1772. int devidx, ret;
  1773. devidx = find_first_zero_bit(dev_use, max_devices);
  1774. if (devidx >= max_devices)
  1775. return ERR_PTR(-ENOSPC);
  1776. __set_bit(devidx, dev_use);
  1777. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1778. if (!md) {
  1779. ret = -ENOMEM;
  1780. goto out;
  1781. }
  1782. /*
  1783. * !subname implies we are creating main mmc_blk_data that will be
  1784. * associated with mmc_card with dev_set_drvdata. Due to device
  1785. * partitions, devidx will not coincide with a per-physical card
  1786. * index anymore so we keep track of a name index.
  1787. */
  1788. if (!subname) {
  1789. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1790. __set_bit(md->name_idx, name_use);
  1791. } else
  1792. md->name_idx = ((struct mmc_blk_data *)
  1793. dev_to_disk(parent)->private_data)->name_idx;
  1794. md->area_type = area_type;
  1795. /*
  1796. * Set the read-only status based on the supported commands
  1797. * and the write protect switch.
  1798. */
  1799. md->read_only = mmc_blk_readonly(card);
  1800. md->disk = alloc_disk(perdev_minors);
  1801. if (md->disk == NULL) {
  1802. ret = -ENOMEM;
  1803. goto err_kfree;
  1804. }
  1805. spin_lock_init(&md->lock);
  1806. INIT_LIST_HEAD(&md->part);
  1807. md->usage = 1;
  1808. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1809. if (ret)
  1810. goto err_putdisk;
  1811. md->queue.issue_fn = mmc_blk_issue_rq;
  1812. md->queue.data = md;
  1813. md->disk->major = MMC_BLOCK_MAJOR;
  1814. md->disk->first_minor = devidx * perdev_minors;
  1815. md->disk->fops = &mmc_bdops;
  1816. md->disk->private_data = md;
  1817. md->disk->queue = md->queue.queue;
  1818. md->disk->driverfs_dev = parent;
  1819. set_disk_ro(md->disk, md->read_only || default_ro);
  1820. if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
  1821. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  1822. /*
  1823. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1824. *
  1825. * - be set for removable media with permanent block devices
  1826. * - be unset for removable block devices with permanent media
  1827. *
  1828. * Since MMC block devices clearly fall under the second
  1829. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1830. * should use the block device creation/destruction hotplug
  1831. * messages to tell when the card is present.
  1832. */
  1833. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1834. "mmcblk%u%s", md->name_idx, subname ? subname : "");
  1835. if (mmc_card_mmc(card))
  1836. blk_queue_logical_block_size(md->queue.queue,
  1837. card->ext_csd.data_sector_size);
  1838. else
  1839. blk_queue_logical_block_size(md->queue.queue, 512);
  1840. set_capacity(md->disk, size);
  1841. if (mmc_host_cmd23(card->host)) {
  1842. if (mmc_card_mmc(card) ||
  1843. (mmc_card_sd(card) &&
  1844. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1845. md->flags |= MMC_BLK_CMD23;
  1846. }
  1847. if (mmc_card_mmc(card) &&
  1848. md->flags & MMC_BLK_CMD23 &&
  1849. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1850. card->ext_csd.rel_sectors)) {
  1851. md->flags |= MMC_BLK_REL_WR;
  1852. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1853. }
  1854. if (mmc_card_mmc(card) &&
  1855. (area_type == MMC_BLK_DATA_AREA_MAIN) &&
  1856. (md->flags & MMC_BLK_CMD23) &&
  1857. card->ext_csd.packed_event_en) {
  1858. if (!mmc_packed_init(&md->queue, card))
  1859. md->flags |= MMC_BLK_PACKED_CMD;
  1860. }
  1861. return md;
  1862. err_putdisk:
  1863. put_disk(md->disk);
  1864. err_kfree:
  1865. kfree(md);
  1866. out:
  1867. return ERR_PTR(ret);
  1868. }
  1869. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1870. {
  1871. sector_t size;
  1872. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1873. /*
  1874. * The EXT_CSD sector count is in number or 512 byte
  1875. * sectors.
  1876. */
  1877. size = card->ext_csd.sectors;
  1878. } else {
  1879. /*
  1880. * The CSD capacity field is in units of read_blkbits.
  1881. * set_capacity takes units of 512 bytes.
  1882. */
  1883. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1884. }
  1885. return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  1886. MMC_BLK_DATA_AREA_MAIN);
  1887. }
  1888. static int mmc_blk_alloc_part(struct mmc_card *card,
  1889. struct mmc_blk_data *md,
  1890. unsigned int part_type,
  1891. sector_t size,
  1892. bool default_ro,
  1893. const char *subname,
  1894. int area_type)
  1895. {
  1896. char cap_str[10];
  1897. struct mmc_blk_data *part_md;
  1898. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1899. subname, area_type);
  1900. if (IS_ERR(part_md))
  1901. return PTR_ERR(part_md);
  1902. part_md->part_type = part_type;
  1903. list_add(&part_md->part, &md->part);
  1904. string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
  1905. cap_str, sizeof(cap_str));
  1906. pr_info("%s: %s %s partition %u %s\n",
  1907. part_md->disk->disk_name, mmc_card_id(card),
  1908. mmc_card_name(card), part_md->part_type, cap_str);
  1909. return 0;
  1910. }
  1911. /* MMC Physical partitions consist of two boot partitions and
  1912. * up to four general purpose partitions.
  1913. * For each partition enabled in EXT_CSD a block device will be allocatedi
  1914. * to provide access to the partition.
  1915. */
  1916. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1917. {
  1918. int idx, ret = 0;
  1919. if (!mmc_card_mmc(card))
  1920. return 0;
  1921. for (idx = 0; idx < card->nr_parts; idx++) {
  1922. if (card->part[idx].size) {
  1923. ret = mmc_blk_alloc_part(card, md,
  1924. card->part[idx].part_cfg,
  1925. card->part[idx].size >> 9,
  1926. card->part[idx].force_ro,
  1927. card->part[idx].name,
  1928. card->part[idx].area_type);
  1929. if (ret)
  1930. return ret;
  1931. }
  1932. }
  1933. return ret;
  1934. }
  1935. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1936. {
  1937. struct mmc_card *card;
  1938. if (md) {
  1939. /*
  1940. * Flush remaining requests and free queues. It
  1941. * is freeing the queue that stops new requests
  1942. * from being accepted.
  1943. */
  1944. card = md->queue.card;
  1945. mmc_cleanup_queue(&md->queue);
  1946. if (md->flags & MMC_BLK_PACKED_CMD)
  1947. mmc_packed_clean(&md->queue);
  1948. if (md->disk->flags & GENHD_FL_UP) {
  1949. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1950. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1951. card->ext_csd.boot_ro_lockable)
  1952. device_remove_file(disk_to_dev(md->disk),
  1953. &md->power_ro_lock);
  1954. del_gendisk(md->disk);
  1955. }
  1956. mmc_blk_put(md);
  1957. }
  1958. }
  1959. static void mmc_blk_remove_parts(struct mmc_card *card,
  1960. struct mmc_blk_data *md)
  1961. {
  1962. struct list_head *pos, *q;
  1963. struct mmc_blk_data *part_md;
  1964. __clear_bit(md->name_idx, name_use);
  1965. list_for_each_safe(pos, q, &md->part) {
  1966. part_md = list_entry(pos, struct mmc_blk_data, part);
  1967. list_del(pos);
  1968. mmc_blk_remove_req(part_md);
  1969. }
  1970. }
  1971. static int mmc_add_disk(struct mmc_blk_data *md)
  1972. {
  1973. int ret;
  1974. struct mmc_card *card = md->queue.card;
  1975. add_disk(md->disk);
  1976. md->force_ro.show = force_ro_show;
  1977. md->force_ro.store = force_ro_store;
  1978. sysfs_attr_init(&md->force_ro.attr);
  1979. md->force_ro.attr.name = "force_ro";
  1980. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1981. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1982. if (ret)
  1983. goto force_ro_fail;
  1984. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1985. card->ext_csd.boot_ro_lockable) {
  1986. umode_t mode;
  1987. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  1988. mode = S_IRUGO;
  1989. else
  1990. mode = S_IRUGO | S_IWUSR;
  1991. md->power_ro_lock.show = power_ro_lock_show;
  1992. md->power_ro_lock.store = power_ro_lock_store;
  1993. sysfs_attr_init(&md->power_ro_lock.attr);
  1994. md->power_ro_lock.attr.mode = mode;
  1995. md->power_ro_lock.attr.name =
  1996. "ro_lock_until_next_power_on";
  1997. ret = device_create_file(disk_to_dev(md->disk),
  1998. &md->power_ro_lock);
  1999. if (ret)
  2000. goto power_ro_lock_fail;
  2001. }
  2002. return ret;
  2003. power_ro_lock_fail:
  2004. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2005. force_ro_fail:
  2006. del_gendisk(md->disk);
  2007. return ret;
  2008. }
  2009. #define CID_MANFID_SANDISK 0x2
  2010. #define CID_MANFID_TOSHIBA 0x11
  2011. #define CID_MANFID_MICRON 0x13
  2012. #define CID_MANFID_SAMSUNG 0x15
  2013. static const struct mmc_fixup blk_fixups[] =
  2014. {
  2015. MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2016. MMC_QUIRK_INAND_CMD38),
  2017. MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2018. MMC_QUIRK_INAND_CMD38),
  2019. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2020. MMC_QUIRK_INAND_CMD38),
  2021. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2022. MMC_QUIRK_INAND_CMD38),
  2023. MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2024. MMC_QUIRK_INAND_CMD38),
  2025. /*
  2026. * Some MMC cards experience performance degradation with CMD23
  2027. * instead of CMD12-bounded multiblock transfers. For now we'll
  2028. * black list what's bad...
  2029. * - Certain Toshiba cards.
  2030. *
  2031. * N.B. This doesn't affect SD cards.
  2032. */
  2033. MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2034. MMC_QUIRK_BLK_NO_CMD23),
  2035. MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2036. MMC_QUIRK_BLK_NO_CMD23),
  2037. MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2038. MMC_QUIRK_BLK_NO_CMD23),
  2039. /*
  2040. * Some Micron MMC cards needs longer data read timeout than
  2041. * indicated in CSD.
  2042. */
  2043. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
  2044. MMC_QUIRK_LONG_READ_TIME),
  2045. /*
  2046. * On these Samsung MoviNAND parts, performing secure erase or
  2047. * secure trim can result in unrecoverable corruption due to a
  2048. * firmware bug.
  2049. */
  2050. MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2051. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2052. MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2053. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2054. MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2055. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2056. MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2057. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2058. MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2059. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2060. MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2061. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2062. MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2063. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2064. MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2065. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2066. END_FIXUP
  2067. };
  2068. static int mmc_blk_probe(struct mmc_card *card)
  2069. {
  2070. struct mmc_blk_data *md, *part_md;
  2071. char cap_str[10];
  2072. /*
  2073. * Check that the card supports the command class(es) we need.
  2074. */
  2075. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  2076. return -ENODEV;
  2077. mmc_fixup_device(card, blk_fixups);
  2078. md = mmc_blk_alloc(card);
  2079. if (IS_ERR(md))
  2080. return PTR_ERR(md);
  2081. string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
  2082. cap_str, sizeof(cap_str));
  2083. pr_info("%s: %s %s %s %s\n",
  2084. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  2085. cap_str, md->read_only ? "(ro)" : "");
  2086. if (mmc_blk_alloc_parts(card, md))
  2087. goto out;
  2088. dev_set_drvdata(&card->dev, md);
  2089. if (mmc_add_disk(md))
  2090. goto out;
  2091. list_for_each_entry(part_md, &md->part, part) {
  2092. if (mmc_add_disk(part_md))
  2093. goto out;
  2094. }
  2095. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  2096. pm_runtime_use_autosuspend(&card->dev);
  2097. /*
  2098. * Don't enable runtime PM for SD-combo cards here. Leave that
  2099. * decision to be taken during the SDIO init sequence instead.
  2100. */
  2101. if (card->type != MMC_TYPE_SD_COMBO) {
  2102. pm_runtime_set_active(&card->dev);
  2103. pm_runtime_enable(&card->dev);
  2104. }
  2105. return 0;
  2106. out:
  2107. mmc_blk_remove_parts(card, md);
  2108. mmc_blk_remove_req(md);
  2109. return 0;
  2110. }
  2111. static void mmc_blk_remove(struct mmc_card *card)
  2112. {
  2113. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2114. mmc_blk_remove_parts(card, md);
  2115. pm_runtime_get_sync(&card->dev);
  2116. mmc_claim_host(card->host);
  2117. mmc_blk_part_switch(card, md);
  2118. mmc_release_host(card->host);
  2119. if (card->type != MMC_TYPE_SD_COMBO)
  2120. pm_runtime_disable(&card->dev);
  2121. pm_runtime_put_noidle(&card->dev);
  2122. mmc_blk_remove_req(md);
  2123. dev_set_drvdata(&card->dev, NULL);
  2124. }
  2125. static int _mmc_blk_suspend(struct mmc_card *card)
  2126. {
  2127. struct mmc_blk_data *part_md;
  2128. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2129. if (md) {
  2130. mmc_queue_suspend(&md->queue);
  2131. list_for_each_entry(part_md, &md->part, part) {
  2132. mmc_queue_suspend(&part_md->queue);
  2133. }
  2134. }
  2135. return 0;
  2136. }
  2137. static void mmc_blk_shutdown(struct mmc_card *card)
  2138. {
  2139. _mmc_blk_suspend(card);
  2140. }
  2141. #ifdef CONFIG_PM_SLEEP
  2142. static int mmc_blk_suspend(struct device *dev)
  2143. {
  2144. struct mmc_card *card = mmc_dev_to_card(dev);
  2145. return _mmc_blk_suspend(card);
  2146. }
  2147. static int mmc_blk_resume(struct device *dev)
  2148. {
  2149. struct mmc_blk_data *part_md;
  2150. struct mmc_blk_data *md = dev_get_drvdata(dev);
  2151. if (md) {
  2152. /*
  2153. * Resume involves the card going into idle state,
  2154. * so current partition is always the main one.
  2155. */
  2156. md->part_curr = md->part_type;
  2157. mmc_queue_resume(&md->queue);
  2158. list_for_each_entry(part_md, &md->part, part) {
  2159. mmc_queue_resume(&part_md->queue);
  2160. }
  2161. }
  2162. return 0;
  2163. }
  2164. #endif
  2165. static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
  2166. static struct mmc_driver mmc_driver = {
  2167. .drv = {
  2168. .name = "mmcblk",
  2169. .pm = &mmc_blk_pm_ops,
  2170. },
  2171. .probe = mmc_blk_probe,
  2172. .remove = mmc_blk_remove,
  2173. .shutdown = mmc_blk_shutdown,
  2174. };
  2175. static int __init mmc_blk_init(void)
  2176. {
  2177. int res;
  2178. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2179. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2180. max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
  2181. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2182. if (res)
  2183. goto out;
  2184. res = mmc_register_driver(&mmc_driver);
  2185. if (res)
  2186. goto out2;
  2187. return 0;
  2188. out2:
  2189. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2190. out:
  2191. return res;
  2192. }
  2193. static void __exit mmc_blk_exit(void)
  2194. {
  2195. mmc_unregister_driver(&mmc_driver);
  2196. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2197. }
  2198. module_init(mmc_blk_init);
  2199. module_exit(mmc_blk_exit);
  2200. MODULE_LICENSE("GPL");
  2201. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");