block.c 60 KB

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