core.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * Copyright (C) 2015 IT University of Copenhagen. All rights reserved.
  3. * Initial release: Matias Bjorling <m@bjorling.me>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version
  7. * 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; see the file COPYING. If not, write to
  16. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  17. * USA.
  18. *
  19. */
  20. #include <linux/list.h>
  21. #include <linux/types.h>
  22. #include <linux/sem.h>
  23. #include <linux/bitmap.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/lightnvm.h>
  28. #include <linux/sched/sysctl.h>
  29. static LIST_HEAD(nvm_tgt_types);
  30. static DECLARE_RWSEM(nvm_tgtt_lock);
  31. static LIST_HEAD(nvm_devices);
  32. static DECLARE_RWSEM(nvm_lock);
  33. /* Map between virtual and physical channel and lun */
  34. struct nvm_ch_map {
  35. int ch_off;
  36. int num_lun;
  37. int *lun_offs;
  38. };
  39. struct nvm_dev_map {
  40. struct nvm_ch_map *chnls;
  41. int num_ch;
  42. };
  43. static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
  44. {
  45. struct nvm_target *tgt;
  46. list_for_each_entry(tgt, &dev->targets, list)
  47. if (!strcmp(name, tgt->disk->disk_name))
  48. return tgt;
  49. return NULL;
  50. }
  51. static bool nvm_target_exists(const char *name)
  52. {
  53. struct nvm_dev *dev;
  54. struct nvm_target *tgt;
  55. bool ret = false;
  56. down_write(&nvm_lock);
  57. list_for_each_entry(dev, &nvm_devices, devices) {
  58. mutex_lock(&dev->mlock);
  59. list_for_each_entry(tgt, &dev->targets, list) {
  60. if (!strcmp(name, tgt->disk->disk_name)) {
  61. ret = true;
  62. mutex_unlock(&dev->mlock);
  63. goto out;
  64. }
  65. }
  66. mutex_unlock(&dev->mlock);
  67. }
  68. out:
  69. up_write(&nvm_lock);
  70. return ret;
  71. }
  72. static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
  73. {
  74. int i;
  75. for (i = lun_begin; i <= lun_end; i++) {
  76. if (test_and_set_bit(i, dev->lun_map)) {
  77. pr_err("nvm: lun %d already allocated\n", i);
  78. goto err;
  79. }
  80. }
  81. return 0;
  82. err:
  83. while (--i >= lun_begin)
  84. clear_bit(i, dev->lun_map);
  85. return -EBUSY;
  86. }
  87. static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
  88. int lun_end)
  89. {
  90. int i;
  91. for (i = lun_begin; i <= lun_end; i++)
  92. WARN_ON(!test_and_clear_bit(i, dev->lun_map));
  93. }
  94. static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
  95. {
  96. struct nvm_dev *dev = tgt_dev->parent;
  97. struct nvm_dev_map *dev_map = tgt_dev->map;
  98. int i, j;
  99. for (i = 0; i < dev_map->num_ch; i++) {
  100. struct nvm_ch_map *ch_map = &dev_map->chnls[i];
  101. int *lun_offs = ch_map->lun_offs;
  102. int ch = i + ch_map->ch_off;
  103. if (clear) {
  104. for (j = 0; j < ch_map->num_lun; j++) {
  105. int lun = j + lun_offs[j];
  106. int lunid = (ch * dev->geo.num_lun) + lun;
  107. WARN_ON(!test_and_clear_bit(lunid,
  108. dev->lun_map));
  109. }
  110. }
  111. kfree(ch_map->lun_offs);
  112. }
  113. kfree(dev_map->chnls);
  114. kfree(dev_map);
  115. kfree(tgt_dev->luns);
  116. kfree(tgt_dev);
  117. }
  118. static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
  119. u16 lun_begin, u16 lun_end,
  120. u16 op)
  121. {
  122. struct nvm_tgt_dev *tgt_dev = NULL;
  123. struct nvm_dev_map *dev_rmap = dev->rmap;
  124. struct nvm_dev_map *dev_map;
  125. struct ppa_addr *luns;
  126. int num_lun = lun_end - lun_begin + 1;
  127. int luns_left = num_lun;
  128. int num_ch = num_lun / dev->geo.num_lun;
  129. int num_ch_mod = num_lun % dev->geo.num_lun;
  130. int bch = lun_begin / dev->geo.num_lun;
  131. int blun = lun_begin % dev->geo.num_lun;
  132. int lunid = 0;
  133. int lun_balanced = 1;
  134. int sec_per_lun, prev_num_lun;
  135. int i, j;
  136. num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1;
  137. dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
  138. if (!dev_map)
  139. goto err_dev;
  140. dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);
  141. if (!dev_map->chnls)
  142. goto err_chnls;
  143. luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);
  144. if (!luns)
  145. goto err_luns;
  146. prev_num_lun = (luns_left > dev->geo.num_lun) ?
  147. dev->geo.num_lun : luns_left;
  148. for (i = 0; i < num_ch; i++) {
  149. struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
  150. int *lun_roffs = ch_rmap->lun_offs;
  151. struct nvm_ch_map *ch_map = &dev_map->chnls[i];
  152. int *lun_offs;
  153. int luns_in_chnl = (luns_left > dev->geo.num_lun) ?
  154. dev->geo.num_lun : luns_left;
  155. if (lun_balanced && prev_num_lun != luns_in_chnl)
  156. lun_balanced = 0;
  157. ch_map->ch_off = ch_rmap->ch_off = bch;
  158. ch_map->num_lun = luns_in_chnl;
  159. lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
  160. if (!lun_offs)
  161. goto err_ch;
  162. for (j = 0; j < luns_in_chnl; j++) {
  163. luns[lunid].ppa = 0;
  164. luns[lunid].a.ch = i;
  165. luns[lunid++].a.lun = j;
  166. lun_offs[j] = blun;
  167. lun_roffs[j + blun] = blun;
  168. }
  169. ch_map->lun_offs = lun_offs;
  170. /* when starting a new channel, lun offset is reset */
  171. blun = 0;
  172. luns_left -= luns_in_chnl;
  173. }
  174. dev_map->num_ch = num_ch;
  175. tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
  176. if (!tgt_dev)
  177. goto err_ch;
  178. /* Inherit device geometry from parent */
  179. memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
  180. /* Target device only owns a portion of the physical device */
  181. tgt_dev->geo.num_ch = num_ch;
  182. tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;
  183. tgt_dev->geo.all_luns = num_lun;
  184. tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk;
  185. tgt_dev->geo.op = op;
  186. sec_per_lun = dev->geo.clba * dev->geo.num_chk;
  187. tgt_dev->geo.total_secs = num_lun * sec_per_lun;
  188. tgt_dev->q = dev->q;
  189. tgt_dev->map = dev_map;
  190. tgt_dev->luns = luns;
  191. tgt_dev->parent = dev;
  192. return tgt_dev;
  193. err_ch:
  194. while (--i >= 0)
  195. kfree(dev_map->chnls[i].lun_offs);
  196. kfree(luns);
  197. err_luns:
  198. kfree(dev_map->chnls);
  199. err_chnls:
  200. kfree(dev_map);
  201. err_dev:
  202. return tgt_dev;
  203. }
  204. static const struct block_device_operations nvm_fops = {
  205. .owner = THIS_MODULE,
  206. };
  207. static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
  208. {
  209. struct nvm_tgt_type *tt;
  210. list_for_each_entry(tt, &nvm_tgt_types, list)
  211. if (!strcmp(name, tt->name))
  212. return tt;
  213. return NULL;
  214. }
  215. static struct nvm_tgt_type *nvm_find_target_type(const char *name)
  216. {
  217. struct nvm_tgt_type *tt;
  218. down_write(&nvm_tgtt_lock);
  219. tt = __nvm_find_target_type(name);
  220. up_write(&nvm_tgtt_lock);
  221. return tt;
  222. }
  223. static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
  224. int lun_end)
  225. {
  226. if (lun_begin > lun_end || lun_end >= geo->all_luns) {
  227. pr_err("nvm: lun out of bound (%u:%u > %u)\n",
  228. lun_begin, lun_end, geo->all_luns - 1);
  229. return -EINVAL;
  230. }
  231. return 0;
  232. }
  233. static int __nvm_config_simple(struct nvm_dev *dev,
  234. struct nvm_ioctl_create_simple *s)
  235. {
  236. struct nvm_geo *geo = &dev->geo;
  237. if (s->lun_begin == -1 && s->lun_end == -1) {
  238. s->lun_begin = 0;
  239. s->lun_end = geo->all_luns - 1;
  240. }
  241. return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
  242. }
  243. static int __nvm_config_extended(struct nvm_dev *dev,
  244. struct nvm_ioctl_create_extended *e)
  245. {
  246. if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
  247. e->lun_begin = 0;
  248. e->lun_end = dev->geo.all_luns - 1;
  249. }
  250. /* op not set falls into target's default */
  251. if (e->op == 0xFFFF) {
  252. e->op = NVM_TARGET_DEFAULT_OP;
  253. } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
  254. pr_err("nvm: invalid over provisioning value\n");
  255. return -EINVAL;
  256. }
  257. return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
  258. }
  259. static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
  260. {
  261. struct nvm_ioctl_create_extended e;
  262. struct request_queue *tqueue;
  263. struct gendisk *tdisk;
  264. struct nvm_tgt_type *tt;
  265. struct nvm_target *t;
  266. struct nvm_tgt_dev *tgt_dev;
  267. void *targetdata;
  268. int ret;
  269. switch (create->conf.type) {
  270. case NVM_CONFIG_TYPE_SIMPLE:
  271. ret = __nvm_config_simple(dev, &create->conf.s);
  272. if (ret)
  273. return ret;
  274. e.lun_begin = create->conf.s.lun_begin;
  275. e.lun_end = create->conf.s.lun_end;
  276. e.op = NVM_TARGET_DEFAULT_OP;
  277. break;
  278. case NVM_CONFIG_TYPE_EXTENDED:
  279. ret = __nvm_config_extended(dev, &create->conf.e);
  280. if (ret)
  281. return ret;
  282. e = create->conf.e;
  283. break;
  284. default:
  285. pr_err("nvm: config type not valid\n");
  286. return -EINVAL;
  287. }
  288. tt = nvm_find_target_type(create->tgttype);
  289. if (!tt) {
  290. pr_err("nvm: target type %s not found\n", create->tgttype);
  291. return -EINVAL;
  292. }
  293. if (nvm_target_exists(create->tgtname)) {
  294. pr_err("nvm: target name already exists (%s)\n",
  295. create->tgtname);
  296. return -EINVAL;
  297. }
  298. ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
  299. if (ret)
  300. return ret;
  301. t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
  302. if (!t) {
  303. ret = -ENOMEM;
  304. goto err_reserve;
  305. }
  306. tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
  307. if (!tgt_dev) {
  308. pr_err("nvm: could not create target device\n");
  309. ret = -ENOMEM;
  310. goto err_t;
  311. }
  312. tdisk = alloc_disk(0);
  313. if (!tdisk) {
  314. ret = -ENOMEM;
  315. goto err_dev;
  316. }
  317. tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node, NULL);
  318. if (!tqueue) {
  319. ret = -ENOMEM;
  320. goto err_disk;
  321. }
  322. blk_queue_make_request(tqueue, tt->make_rq);
  323. strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
  324. tdisk->flags = GENHD_FL_EXT_DEVT;
  325. tdisk->major = 0;
  326. tdisk->first_minor = 0;
  327. tdisk->fops = &nvm_fops;
  328. tdisk->queue = tqueue;
  329. targetdata = tt->init(tgt_dev, tdisk, create->flags);
  330. if (IS_ERR(targetdata)) {
  331. ret = PTR_ERR(targetdata);
  332. goto err_init;
  333. }
  334. tdisk->private_data = targetdata;
  335. tqueue->queuedata = targetdata;
  336. blk_queue_max_hw_sectors(tqueue,
  337. (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
  338. set_capacity(tdisk, tt->capacity(targetdata));
  339. add_disk(tdisk);
  340. if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
  341. ret = -ENOMEM;
  342. goto err_sysfs;
  343. }
  344. t->type = tt;
  345. t->disk = tdisk;
  346. t->dev = tgt_dev;
  347. mutex_lock(&dev->mlock);
  348. list_add_tail(&t->list, &dev->targets);
  349. mutex_unlock(&dev->mlock);
  350. __module_get(tt->owner);
  351. return 0;
  352. err_sysfs:
  353. if (tt->exit)
  354. tt->exit(targetdata, true);
  355. err_init:
  356. blk_cleanup_queue(tqueue);
  357. tdisk->queue = NULL;
  358. err_disk:
  359. put_disk(tdisk);
  360. err_dev:
  361. nvm_remove_tgt_dev(tgt_dev, 0);
  362. err_t:
  363. kfree(t);
  364. err_reserve:
  365. nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
  366. return ret;
  367. }
  368. static void __nvm_remove_target(struct nvm_target *t, bool graceful)
  369. {
  370. struct nvm_tgt_type *tt = t->type;
  371. struct gendisk *tdisk = t->disk;
  372. struct request_queue *q = tdisk->queue;
  373. del_gendisk(tdisk);
  374. blk_cleanup_queue(q);
  375. if (tt->sysfs_exit)
  376. tt->sysfs_exit(tdisk);
  377. if (tt->exit)
  378. tt->exit(tdisk->private_data, graceful);
  379. nvm_remove_tgt_dev(t->dev, 1);
  380. put_disk(tdisk);
  381. module_put(t->type->owner);
  382. list_del(&t->list);
  383. kfree(t);
  384. }
  385. /**
  386. * nvm_remove_tgt - Removes a target from the media manager
  387. * @dev: device
  388. * @remove: ioctl structure with target name to remove.
  389. *
  390. * Returns:
  391. * 0: on success
  392. * 1: on not found
  393. * <0: on error
  394. */
  395. static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
  396. {
  397. struct nvm_target *t;
  398. mutex_lock(&dev->mlock);
  399. t = nvm_find_target(dev, remove->tgtname);
  400. if (!t) {
  401. mutex_unlock(&dev->mlock);
  402. return 1;
  403. }
  404. __nvm_remove_target(t, true);
  405. mutex_unlock(&dev->mlock);
  406. return 0;
  407. }
  408. static int nvm_register_map(struct nvm_dev *dev)
  409. {
  410. struct nvm_dev_map *rmap;
  411. int i, j;
  412. rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
  413. if (!rmap)
  414. goto err_rmap;
  415. rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map),
  416. GFP_KERNEL);
  417. if (!rmap->chnls)
  418. goto err_chnls;
  419. for (i = 0; i < dev->geo.num_ch; i++) {
  420. struct nvm_ch_map *ch_rmap;
  421. int *lun_roffs;
  422. int luns_in_chnl = dev->geo.num_lun;
  423. ch_rmap = &rmap->chnls[i];
  424. ch_rmap->ch_off = -1;
  425. ch_rmap->num_lun = luns_in_chnl;
  426. lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
  427. if (!lun_roffs)
  428. goto err_ch;
  429. for (j = 0; j < luns_in_chnl; j++)
  430. lun_roffs[j] = -1;
  431. ch_rmap->lun_offs = lun_roffs;
  432. }
  433. dev->rmap = rmap;
  434. return 0;
  435. err_ch:
  436. while (--i >= 0)
  437. kfree(rmap->chnls[i].lun_offs);
  438. err_chnls:
  439. kfree(rmap);
  440. err_rmap:
  441. return -ENOMEM;
  442. }
  443. static void nvm_unregister_map(struct nvm_dev *dev)
  444. {
  445. struct nvm_dev_map *rmap = dev->rmap;
  446. int i;
  447. for (i = 0; i < dev->geo.num_ch; i++)
  448. kfree(rmap->chnls[i].lun_offs);
  449. kfree(rmap->chnls);
  450. kfree(rmap);
  451. }
  452. static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
  453. {
  454. struct nvm_dev_map *dev_map = tgt_dev->map;
  455. struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch];
  456. int lun_off = ch_map->lun_offs[p->a.lun];
  457. p->a.ch += ch_map->ch_off;
  458. p->a.lun += lun_off;
  459. }
  460. static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
  461. {
  462. struct nvm_dev *dev = tgt_dev->parent;
  463. struct nvm_dev_map *dev_rmap = dev->rmap;
  464. struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch];
  465. int lun_roff = ch_rmap->lun_offs[p->a.lun];
  466. p->a.ch -= ch_rmap->ch_off;
  467. p->a.lun -= lun_roff;
  468. }
  469. static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
  470. struct ppa_addr *ppa_list, int nr_ppas)
  471. {
  472. int i;
  473. for (i = 0; i < nr_ppas; i++) {
  474. nvm_map_to_dev(tgt_dev, &ppa_list[i]);
  475. ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]);
  476. }
  477. }
  478. static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
  479. struct ppa_addr *ppa_list, int nr_ppas)
  480. {
  481. int i;
  482. for (i = 0; i < nr_ppas; i++) {
  483. ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]);
  484. nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
  485. }
  486. }
  487. static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  488. {
  489. if (rqd->nr_ppas == 1) {
  490. nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
  491. return;
  492. }
  493. nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
  494. }
  495. static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  496. {
  497. if (rqd->nr_ppas == 1) {
  498. nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
  499. return;
  500. }
  501. nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
  502. }
  503. int nvm_register_tgt_type(struct nvm_tgt_type *tt)
  504. {
  505. int ret = 0;
  506. down_write(&nvm_tgtt_lock);
  507. if (__nvm_find_target_type(tt->name))
  508. ret = -EEXIST;
  509. else
  510. list_add(&tt->list, &nvm_tgt_types);
  511. up_write(&nvm_tgtt_lock);
  512. return ret;
  513. }
  514. EXPORT_SYMBOL(nvm_register_tgt_type);
  515. void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
  516. {
  517. if (!tt)
  518. return;
  519. down_write(&nvm_tgtt_lock);
  520. list_del(&tt->list);
  521. up_write(&nvm_tgtt_lock);
  522. }
  523. EXPORT_SYMBOL(nvm_unregister_tgt_type);
  524. void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
  525. dma_addr_t *dma_handler)
  526. {
  527. return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
  528. dma_handler);
  529. }
  530. EXPORT_SYMBOL(nvm_dev_dma_alloc);
  531. void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
  532. {
  533. dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
  534. }
  535. EXPORT_SYMBOL(nvm_dev_dma_free);
  536. static struct nvm_dev *nvm_find_nvm_dev(const char *name)
  537. {
  538. struct nvm_dev *dev;
  539. list_for_each_entry(dev, &nvm_devices, devices)
  540. if (!strcmp(name, dev->name))
  541. return dev;
  542. return NULL;
  543. }
  544. static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
  545. const struct ppa_addr *ppas, int nr_ppas)
  546. {
  547. struct nvm_dev *dev = tgt_dev->parent;
  548. struct nvm_geo *geo = &tgt_dev->geo;
  549. int i, plane_cnt, pl_idx;
  550. struct ppa_addr ppa;
  551. if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
  552. rqd->nr_ppas = nr_ppas;
  553. rqd->ppa_addr = ppas[0];
  554. return 0;
  555. }
  556. rqd->nr_ppas = nr_ppas;
  557. rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
  558. if (!rqd->ppa_list) {
  559. pr_err("nvm: failed to allocate dma memory\n");
  560. return -ENOMEM;
  561. }
  562. plane_cnt = geo->pln_mode;
  563. rqd->nr_ppas *= plane_cnt;
  564. for (i = 0; i < nr_ppas; i++) {
  565. for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
  566. ppa = ppas[i];
  567. ppa.g.pl = pl_idx;
  568. rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
  569. }
  570. }
  571. return 0;
  572. }
  573. static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
  574. struct nvm_rq *rqd)
  575. {
  576. if (!rqd->ppa_list)
  577. return;
  578. nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
  579. }
  580. int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct nvm_chk_meta *meta,
  581. struct ppa_addr ppa, int nchks)
  582. {
  583. struct nvm_dev *dev = tgt_dev->parent;
  584. nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
  585. return dev->ops->get_chk_meta(tgt_dev->parent, meta,
  586. (sector_t)ppa.ppa, nchks);
  587. }
  588. EXPORT_SYMBOL(nvm_get_chunk_meta);
  589. int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
  590. int nr_ppas, int type)
  591. {
  592. struct nvm_dev *dev = tgt_dev->parent;
  593. struct nvm_rq rqd;
  594. int ret;
  595. if (nr_ppas > NVM_MAX_VLBA) {
  596. pr_err("nvm: unable to update all blocks atomically\n");
  597. return -EINVAL;
  598. }
  599. memset(&rqd, 0, sizeof(struct nvm_rq));
  600. nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
  601. nvm_rq_tgt_to_dev(tgt_dev, &rqd);
  602. ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
  603. nvm_free_rqd_ppalist(tgt_dev, &rqd);
  604. if (ret) {
  605. pr_err("nvm: failed bb mark\n");
  606. return -EINVAL;
  607. }
  608. return 0;
  609. }
  610. EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
  611. int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  612. {
  613. struct nvm_dev *dev = tgt_dev->parent;
  614. int ret;
  615. if (!dev->ops->submit_io)
  616. return -ENODEV;
  617. nvm_rq_tgt_to_dev(tgt_dev, rqd);
  618. rqd->dev = tgt_dev;
  619. /* In case of error, fail with right address format */
  620. ret = dev->ops->submit_io(dev, rqd);
  621. if (ret)
  622. nvm_rq_dev_to_tgt(tgt_dev, rqd);
  623. return ret;
  624. }
  625. EXPORT_SYMBOL(nvm_submit_io);
  626. int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  627. {
  628. struct nvm_dev *dev = tgt_dev->parent;
  629. int ret;
  630. if (!dev->ops->submit_io_sync)
  631. return -ENODEV;
  632. nvm_rq_tgt_to_dev(tgt_dev, rqd);
  633. rqd->dev = tgt_dev;
  634. /* In case of error, fail with right address format */
  635. ret = dev->ops->submit_io_sync(dev, rqd);
  636. nvm_rq_dev_to_tgt(tgt_dev, rqd);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL(nvm_submit_io_sync);
  640. void nvm_end_io(struct nvm_rq *rqd)
  641. {
  642. struct nvm_tgt_dev *tgt_dev = rqd->dev;
  643. /* Convert address space */
  644. if (tgt_dev)
  645. nvm_rq_dev_to_tgt(tgt_dev, rqd);
  646. if (rqd->end_io)
  647. rqd->end_io(rqd);
  648. }
  649. EXPORT_SYMBOL(nvm_end_io);
  650. /*
  651. * folds a bad block list from its plane representation to its virtual
  652. * block representation. The fold is done in place and reduced size is
  653. * returned.
  654. *
  655. * If any of the planes status are bad or grown bad block, the virtual block
  656. * is marked bad. If not bad, the first plane state acts as the block state.
  657. */
  658. int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
  659. {
  660. struct nvm_geo *geo = &dev->geo;
  661. int blk, offset, pl, blktype;
  662. if (nr_blks != geo->num_chk * geo->pln_mode)
  663. return -EINVAL;
  664. for (blk = 0; blk < geo->num_chk; blk++) {
  665. offset = blk * geo->pln_mode;
  666. blktype = blks[offset];
  667. /* Bad blocks on any planes take precedence over other types */
  668. for (pl = 0; pl < geo->pln_mode; pl++) {
  669. if (blks[offset + pl] &
  670. (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
  671. blktype = blks[offset + pl];
  672. break;
  673. }
  674. }
  675. blks[blk] = blktype;
  676. }
  677. return geo->num_chk;
  678. }
  679. EXPORT_SYMBOL(nvm_bb_tbl_fold);
  680. int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
  681. u8 *blks)
  682. {
  683. struct nvm_dev *dev = tgt_dev->parent;
  684. nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
  685. return dev->ops->get_bb_tbl(dev, ppa, blks);
  686. }
  687. EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
  688. static int nvm_core_init(struct nvm_dev *dev)
  689. {
  690. struct nvm_geo *geo = &dev->geo;
  691. int ret;
  692. dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
  693. sizeof(unsigned long), GFP_KERNEL);
  694. if (!dev->lun_map)
  695. return -ENOMEM;
  696. INIT_LIST_HEAD(&dev->area_list);
  697. INIT_LIST_HEAD(&dev->targets);
  698. mutex_init(&dev->mlock);
  699. spin_lock_init(&dev->lock);
  700. ret = nvm_register_map(dev);
  701. if (ret)
  702. goto err_fmtype;
  703. return 0;
  704. err_fmtype:
  705. kfree(dev->lun_map);
  706. return ret;
  707. }
  708. static void nvm_free(struct nvm_dev *dev)
  709. {
  710. if (!dev)
  711. return;
  712. if (dev->dma_pool)
  713. dev->ops->destroy_dma_pool(dev->dma_pool);
  714. nvm_unregister_map(dev);
  715. kfree(dev->lun_map);
  716. kfree(dev);
  717. }
  718. static int nvm_init(struct nvm_dev *dev)
  719. {
  720. struct nvm_geo *geo = &dev->geo;
  721. int ret = -EINVAL;
  722. if (dev->ops->identity(dev)) {
  723. pr_err("nvm: device could not be identified\n");
  724. goto err;
  725. }
  726. pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n",
  727. geo->major_ver_id, geo->minor_ver_id,
  728. geo->vmnt);
  729. ret = nvm_core_init(dev);
  730. if (ret) {
  731. pr_err("nvm: could not initialize core structures.\n");
  732. goto err;
  733. }
  734. pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n",
  735. dev->name, dev->geo.ws_min, dev->geo.ws_opt,
  736. dev->geo.num_chk, dev->geo.all_luns,
  737. dev->geo.num_ch);
  738. return 0;
  739. err:
  740. pr_err("nvm: failed to initialize nvm\n");
  741. return ret;
  742. }
  743. struct nvm_dev *nvm_alloc_dev(int node)
  744. {
  745. return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
  746. }
  747. EXPORT_SYMBOL(nvm_alloc_dev);
  748. int nvm_register(struct nvm_dev *dev)
  749. {
  750. int ret;
  751. if (!dev->q || !dev->ops)
  752. return -EINVAL;
  753. dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
  754. if (!dev->dma_pool) {
  755. pr_err("nvm: could not create dma pool\n");
  756. return -ENOMEM;
  757. }
  758. ret = nvm_init(dev);
  759. if (ret)
  760. goto err_init;
  761. /* register device with a supported media manager */
  762. down_write(&nvm_lock);
  763. list_add(&dev->devices, &nvm_devices);
  764. up_write(&nvm_lock);
  765. return 0;
  766. err_init:
  767. dev->ops->destroy_dma_pool(dev->dma_pool);
  768. return ret;
  769. }
  770. EXPORT_SYMBOL(nvm_register);
  771. void nvm_unregister(struct nvm_dev *dev)
  772. {
  773. struct nvm_target *t, *tmp;
  774. mutex_lock(&dev->mlock);
  775. list_for_each_entry_safe(t, tmp, &dev->targets, list) {
  776. if (t->dev->parent != dev)
  777. continue;
  778. __nvm_remove_target(t, false);
  779. }
  780. mutex_unlock(&dev->mlock);
  781. down_write(&nvm_lock);
  782. list_del(&dev->devices);
  783. up_write(&nvm_lock);
  784. nvm_free(dev);
  785. }
  786. EXPORT_SYMBOL(nvm_unregister);
  787. static int __nvm_configure_create(struct nvm_ioctl_create *create)
  788. {
  789. struct nvm_dev *dev;
  790. down_write(&nvm_lock);
  791. dev = nvm_find_nvm_dev(create->dev);
  792. up_write(&nvm_lock);
  793. if (!dev) {
  794. pr_err("nvm: device not found\n");
  795. return -EINVAL;
  796. }
  797. return nvm_create_tgt(dev, create);
  798. }
  799. static long nvm_ioctl_info(struct file *file, void __user *arg)
  800. {
  801. struct nvm_ioctl_info *info;
  802. struct nvm_tgt_type *tt;
  803. int tgt_iter = 0;
  804. info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
  805. if (IS_ERR(info))
  806. return -EFAULT;
  807. info->version[0] = NVM_VERSION_MAJOR;
  808. info->version[1] = NVM_VERSION_MINOR;
  809. info->version[2] = NVM_VERSION_PATCH;
  810. down_write(&nvm_tgtt_lock);
  811. list_for_each_entry(tt, &nvm_tgt_types, list) {
  812. struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
  813. tgt->version[0] = tt->version[0];
  814. tgt->version[1] = tt->version[1];
  815. tgt->version[2] = tt->version[2];
  816. strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
  817. tgt_iter++;
  818. }
  819. info->tgtsize = tgt_iter;
  820. up_write(&nvm_tgtt_lock);
  821. if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
  822. kfree(info);
  823. return -EFAULT;
  824. }
  825. kfree(info);
  826. return 0;
  827. }
  828. static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
  829. {
  830. struct nvm_ioctl_get_devices *devices;
  831. struct nvm_dev *dev;
  832. int i = 0;
  833. devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
  834. if (!devices)
  835. return -ENOMEM;
  836. down_write(&nvm_lock);
  837. list_for_each_entry(dev, &nvm_devices, devices) {
  838. struct nvm_ioctl_device_info *info = &devices->info[i];
  839. strlcpy(info->devname, dev->name, sizeof(info->devname));
  840. /* kept for compatibility */
  841. info->bmversion[0] = 1;
  842. info->bmversion[1] = 0;
  843. info->bmversion[2] = 0;
  844. strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
  845. i++;
  846. if (i > 31) {
  847. pr_err("nvm: max 31 devices can be reported.\n");
  848. break;
  849. }
  850. }
  851. up_write(&nvm_lock);
  852. devices->nr_devices = i;
  853. if (copy_to_user(arg, devices,
  854. sizeof(struct nvm_ioctl_get_devices))) {
  855. kfree(devices);
  856. return -EFAULT;
  857. }
  858. kfree(devices);
  859. return 0;
  860. }
  861. static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
  862. {
  863. struct nvm_ioctl_create create;
  864. if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
  865. return -EFAULT;
  866. if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
  867. create.conf.e.rsv != 0) {
  868. pr_err("nvm: reserved config field in use\n");
  869. return -EINVAL;
  870. }
  871. create.dev[DISK_NAME_LEN - 1] = '\0';
  872. create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
  873. create.tgtname[DISK_NAME_LEN - 1] = '\0';
  874. if (create.flags != 0) {
  875. __u32 flags = create.flags;
  876. /* Check for valid flags */
  877. if (flags & NVM_TARGET_FACTORY)
  878. flags &= ~NVM_TARGET_FACTORY;
  879. if (flags) {
  880. pr_err("nvm: flag not supported\n");
  881. return -EINVAL;
  882. }
  883. }
  884. return __nvm_configure_create(&create);
  885. }
  886. static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
  887. {
  888. struct nvm_ioctl_remove remove;
  889. struct nvm_dev *dev;
  890. int ret = 0;
  891. if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
  892. return -EFAULT;
  893. remove.tgtname[DISK_NAME_LEN - 1] = '\0';
  894. if (remove.flags != 0) {
  895. pr_err("nvm: no flags supported\n");
  896. return -EINVAL;
  897. }
  898. list_for_each_entry(dev, &nvm_devices, devices) {
  899. ret = nvm_remove_tgt(dev, &remove);
  900. if (!ret)
  901. break;
  902. }
  903. return ret;
  904. }
  905. /* kept for compatibility reasons */
  906. static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
  907. {
  908. struct nvm_ioctl_dev_init init;
  909. if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
  910. return -EFAULT;
  911. if (init.flags != 0) {
  912. pr_err("nvm: no flags supported\n");
  913. return -EINVAL;
  914. }
  915. return 0;
  916. }
  917. /* Kept for compatibility reasons */
  918. static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
  919. {
  920. struct nvm_ioctl_dev_factory fact;
  921. if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
  922. return -EFAULT;
  923. fact.dev[DISK_NAME_LEN - 1] = '\0';
  924. if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
  925. return -EINVAL;
  926. return 0;
  927. }
  928. static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
  929. {
  930. void __user *argp = (void __user *)arg;
  931. if (!capable(CAP_SYS_ADMIN))
  932. return -EPERM;
  933. switch (cmd) {
  934. case NVM_INFO:
  935. return nvm_ioctl_info(file, argp);
  936. case NVM_GET_DEVICES:
  937. return nvm_ioctl_get_devices(file, argp);
  938. case NVM_DEV_CREATE:
  939. return nvm_ioctl_dev_create(file, argp);
  940. case NVM_DEV_REMOVE:
  941. return nvm_ioctl_dev_remove(file, argp);
  942. case NVM_DEV_INIT:
  943. return nvm_ioctl_dev_init(file, argp);
  944. case NVM_DEV_FACTORY:
  945. return nvm_ioctl_dev_factory(file, argp);
  946. }
  947. return 0;
  948. }
  949. static const struct file_operations _ctl_fops = {
  950. .open = nonseekable_open,
  951. .unlocked_ioctl = nvm_ctl_ioctl,
  952. .owner = THIS_MODULE,
  953. .llseek = noop_llseek,
  954. };
  955. static struct miscdevice _nvm_misc = {
  956. .minor = MISC_DYNAMIC_MINOR,
  957. .name = "lightnvm",
  958. .nodename = "lightnvm/control",
  959. .fops = &_ctl_fops,
  960. };
  961. builtin_misc_device(_nvm_misc);