block.c 63 KB

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