core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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/moduleparam.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/lightnvm.h>
  27. #include <linux/sched/sysctl.h>
  28. static LIST_HEAD(nvm_tgt_types);
  29. static DECLARE_RWSEM(nvm_tgtt_lock);
  30. static LIST_HEAD(nvm_devices);
  31. static DECLARE_RWSEM(nvm_lock);
  32. /* Map between virtual and physical channel and lun */
  33. struct nvm_ch_map {
  34. int ch_off;
  35. int nr_luns;
  36. int *lun_offs;
  37. };
  38. struct nvm_dev_map {
  39. struct nvm_ch_map *chnls;
  40. int nr_chnls;
  41. };
  42. struct nvm_area {
  43. struct list_head list;
  44. sector_t begin;
  45. sector_t end; /* end is excluded */
  46. };
  47. static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
  48. {
  49. struct nvm_target *tgt;
  50. list_for_each_entry(tgt, &dev->targets, list)
  51. if (!strcmp(name, tgt->disk->disk_name))
  52. return tgt;
  53. return NULL;
  54. }
  55. static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
  56. {
  57. int i;
  58. for (i = lun_begin; i <= lun_end; i++) {
  59. if (test_and_set_bit(i, dev->lun_map)) {
  60. pr_err("nvm: lun %d already allocated\n", i);
  61. goto err;
  62. }
  63. }
  64. return 0;
  65. err:
  66. while (--i >= lun_begin)
  67. clear_bit(i, dev->lun_map);
  68. return -EBUSY;
  69. }
  70. static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
  71. int lun_end)
  72. {
  73. int i;
  74. for (i = lun_begin; i <= lun_end; i++)
  75. WARN_ON(!test_and_clear_bit(i, dev->lun_map));
  76. }
  77. static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
  78. {
  79. struct nvm_dev *dev = tgt_dev->parent;
  80. struct nvm_dev_map *dev_map = tgt_dev->map;
  81. int i, j;
  82. for (i = 0; i < dev_map->nr_chnls; i++) {
  83. struct nvm_ch_map *ch_map = &dev_map->chnls[i];
  84. int *lun_offs = ch_map->lun_offs;
  85. int ch = i + ch_map->ch_off;
  86. if (clear) {
  87. for (j = 0; j < ch_map->nr_luns; j++) {
  88. int lun = j + lun_offs[j];
  89. int lunid = (ch * dev->geo.luns_per_chnl) + lun;
  90. WARN_ON(!test_and_clear_bit(lunid,
  91. dev->lun_map));
  92. }
  93. }
  94. kfree(ch_map->lun_offs);
  95. }
  96. kfree(dev_map->chnls);
  97. kfree(dev_map);
  98. kfree(tgt_dev->luns);
  99. kfree(tgt_dev);
  100. }
  101. static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
  102. int lun_begin, int lun_end)
  103. {
  104. struct nvm_tgt_dev *tgt_dev = NULL;
  105. struct nvm_dev_map *dev_rmap = dev->rmap;
  106. struct nvm_dev_map *dev_map;
  107. struct ppa_addr *luns;
  108. int nr_luns = lun_end - lun_begin + 1;
  109. int luns_left = nr_luns;
  110. int nr_chnls = nr_luns / dev->geo.luns_per_chnl;
  111. int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl;
  112. int bch = lun_begin / dev->geo.luns_per_chnl;
  113. int blun = lun_begin % dev->geo.luns_per_chnl;
  114. int lunid = 0;
  115. int lun_balanced = 1;
  116. int prev_nr_luns;
  117. int i, j;
  118. nr_chnls = nr_luns / dev->geo.luns_per_chnl;
  119. nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
  120. dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
  121. if (!dev_map)
  122. goto err_dev;
  123. dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
  124. GFP_KERNEL);
  125. if (!dev_map->chnls)
  126. goto err_chnls;
  127. luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
  128. if (!luns)
  129. goto err_luns;
  130. prev_nr_luns = (luns_left > dev->geo.luns_per_chnl) ?
  131. dev->geo.luns_per_chnl : luns_left;
  132. for (i = 0; i < nr_chnls; i++) {
  133. struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
  134. int *lun_roffs = ch_rmap->lun_offs;
  135. struct nvm_ch_map *ch_map = &dev_map->chnls[i];
  136. int *lun_offs;
  137. int luns_in_chnl = (luns_left > dev->geo.luns_per_chnl) ?
  138. dev->geo.luns_per_chnl : luns_left;
  139. if (lun_balanced && prev_nr_luns != luns_in_chnl)
  140. lun_balanced = 0;
  141. ch_map->ch_off = ch_rmap->ch_off = bch;
  142. ch_map->nr_luns = luns_in_chnl;
  143. lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
  144. if (!lun_offs)
  145. goto err_ch;
  146. for (j = 0; j < luns_in_chnl; j++) {
  147. luns[lunid].ppa = 0;
  148. luns[lunid].g.ch = i;
  149. luns[lunid++].g.lun = j;
  150. lun_offs[j] = blun;
  151. lun_roffs[j + blun] = blun;
  152. }
  153. ch_map->lun_offs = lun_offs;
  154. /* when starting a new channel, lun offset is reset */
  155. blun = 0;
  156. luns_left -= luns_in_chnl;
  157. }
  158. dev_map->nr_chnls = nr_chnls;
  159. tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
  160. if (!tgt_dev)
  161. goto err_ch;
  162. memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
  163. /* Target device only owns a portion of the physical device */
  164. tgt_dev->geo.nr_chnls = nr_chnls;
  165. tgt_dev->geo.nr_luns = nr_luns;
  166. tgt_dev->geo.luns_per_chnl = (lun_balanced) ? prev_nr_luns : -1;
  167. tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
  168. tgt_dev->q = dev->q;
  169. tgt_dev->map = dev_map;
  170. tgt_dev->luns = luns;
  171. memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
  172. tgt_dev->parent = dev;
  173. return tgt_dev;
  174. err_ch:
  175. while (--i >= 0)
  176. kfree(dev_map->chnls[i].lun_offs);
  177. kfree(luns);
  178. err_luns:
  179. kfree(dev_map->chnls);
  180. err_chnls:
  181. kfree(dev_map);
  182. err_dev:
  183. return tgt_dev;
  184. }
  185. static const struct block_device_operations nvm_fops = {
  186. .owner = THIS_MODULE,
  187. };
  188. static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
  189. {
  190. struct nvm_ioctl_create_simple *s = &create->conf.s;
  191. struct request_queue *tqueue;
  192. struct gendisk *tdisk;
  193. struct nvm_tgt_type *tt;
  194. struct nvm_target *t;
  195. struct nvm_tgt_dev *tgt_dev;
  196. void *targetdata;
  197. int ret;
  198. tt = nvm_find_target_type(create->tgttype, 1);
  199. if (!tt) {
  200. pr_err("nvm: target type %s not found\n", create->tgttype);
  201. return -EINVAL;
  202. }
  203. mutex_lock(&dev->mlock);
  204. t = nvm_find_target(dev, create->tgtname);
  205. if (t) {
  206. pr_err("nvm: target name already exists.\n");
  207. mutex_unlock(&dev->mlock);
  208. return -EINVAL;
  209. }
  210. mutex_unlock(&dev->mlock);
  211. ret = nvm_reserve_luns(dev, s->lun_begin, s->lun_end);
  212. if (ret)
  213. return ret;
  214. t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
  215. if (!t) {
  216. ret = -ENOMEM;
  217. goto err_reserve;
  218. }
  219. tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
  220. if (!tgt_dev) {
  221. pr_err("nvm: could not create target device\n");
  222. ret = -ENOMEM;
  223. goto err_t;
  224. }
  225. tdisk = alloc_disk(0);
  226. if (!tdisk) {
  227. ret = -ENOMEM;
  228. goto err_dev;
  229. }
  230. tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
  231. if (!tqueue) {
  232. ret = -ENOMEM;
  233. goto err_disk;
  234. }
  235. blk_queue_make_request(tqueue, tt->make_rq);
  236. strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
  237. tdisk->flags = GENHD_FL_EXT_DEVT;
  238. tdisk->major = 0;
  239. tdisk->first_minor = 0;
  240. tdisk->fops = &nvm_fops;
  241. tdisk->queue = tqueue;
  242. targetdata = tt->init(tgt_dev, tdisk, create->flags);
  243. if (IS_ERR(targetdata)) {
  244. ret = PTR_ERR(targetdata);
  245. goto err_init;
  246. }
  247. tdisk->private_data = targetdata;
  248. tqueue->queuedata = targetdata;
  249. blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
  250. set_capacity(tdisk, tt->capacity(targetdata));
  251. add_disk(tdisk);
  252. if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
  253. ret = -ENOMEM;
  254. goto err_sysfs;
  255. }
  256. t->type = tt;
  257. t->disk = tdisk;
  258. t->dev = tgt_dev;
  259. mutex_lock(&dev->mlock);
  260. list_add_tail(&t->list, &dev->targets);
  261. mutex_unlock(&dev->mlock);
  262. return 0;
  263. err_sysfs:
  264. if (tt->exit)
  265. tt->exit(targetdata);
  266. err_init:
  267. blk_cleanup_queue(tqueue);
  268. tdisk->queue = NULL;
  269. err_disk:
  270. put_disk(tdisk);
  271. err_dev:
  272. nvm_remove_tgt_dev(tgt_dev, 0);
  273. err_t:
  274. kfree(t);
  275. err_reserve:
  276. nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
  277. return ret;
  278. }
  279. static void __nvm_remove_target(struct nvm_target *t)
  280. {
  281. struct nvm_tgt_type *tt = t->type;
  282. struct gendisk *tdisk = t->disk;
  283. struct request_queue *q = tdisk->queue;
  284. del_gendisk(tdisk);
  285. blk_cleanup_queue(q);
  286. if (tt->sysfs_exit)
  287. tt->sysfs_exit(tdisk);
  288. if (tt->exit)
  289. tt->exit(tdisk->private_data);
  290. nvm_remove_tgt_dev(t->dev, 1);
  291. put_disk(tdisk);
  292. list_del(&t->list);
  293. kfree(t);
  294. }
  295. /**
  296. * nvm_remove_tgt - Removes a target from the media manager
  297. * @dev: device
  298. * @remove: ioctl structure with target name to remove.
  299. *
  300. * Returns:
  301. * 0: on success
  302. * 1: on not found
  303. * <0: on error
  304. */
  305. static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
  306. {
  307. struct nvm_target *t;
  308. mutex_lock(&dev->mlock);
  309. t = nvm_find_target(dev, remove->tgtname);
  310. if (!t) {
  311. mutex_unlock(&dev->mlock);
  312. return 1;
  313. }
  314. __nvm_remove_target(t);
  315. mutex_unlock(&dev->mlock);
  316. return 0;
  317. }
  318. static int nvm_register_map(struct nvm_dev *dev)
  319. {
  320. struct nvm_dev_map *rmap;
  321. int i, j;
  322. rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
  323. if (!rmap)
  324. goto err_rmap;
  325. rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
  326. GFP_KERNEL);
  327. if (!rmap->chnls)
  328. goto err_chnls;
  329. for (i = 0; i < dev->geo.nr_chnls; i++) {
  330. struct nvm_ch_map *ch_rmap;
  331. int *lun_roffs;
  332. int luns_in_chnl = dev->geo.luns_per_chnl;
  333. ch_rmap = &rmap->chnls[i];
  334. ch_rmap->ch_off = -1;
  335. ch_rmap->nr_luns = luns_in_chnl;
  336. lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
  337. if (!lun_roffs)
  338. goto err_ch;
  339. for (j = 0; j < luns_in_chnl; j++)
  340. lun_roffs[j] = -1;
  341. ch_rmap->lun_offs = lun_roffs;
  342. }
  343. dev->rmap = rmap;
  344. return 0;
  345. err_ch:
  346. while (--i >= 0)
  347. kfree(rmap->chnls[i].lun_offs);
  348. err_chnls:
  349. kfree(rmap);
  350. err_rmap:
  351. return -ENOMEM;
  352. }
  353. static void nvm_unregister_map(struct nvm_dev *dev)
  354. {
  355. struct nvm_dev_map *rmap = dev->rmap;
  356. int i;
  357. for (i = 0; i < dev->geo.nr_chnls; i++)
  358. kfree(rmap->chnls[i].lun_offs);
  359. kfree(rmap->chnls);
  360. kfree(rmap);
  361. }
  362. static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
  363. {
  364. struct nvm_dev_map *dev_map = tgt_dev->map;
  365. struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
  366. int lun_off = ch_map->lun_offs[p->g.lun];
  367. p->g.ch += ch_map->ch_off;
  368. p->g.lun += lun_off;
  369. }
  370. static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
  371. {
  372. struct nvm_dev *dev = tgt_dev->parent;
  373. struct nvm_dev_map *dev_rmap = dev->rmap;
  374. struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
  375. int lun_roff = ch_rmap->lun_offs[p->g.lun];
  376. p->g.ch -= ch_rmap->ch_off;
  377. p->g.lun -= lun_roff;
  378. }
  379. static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
  380. struct ppa_addr *ppa_list, int nr_ppas)
  381. {
  382. int i;
  383. for (i = 0; i < nr_ppas; i++) {
  384. nvm_map_to_dev(tgt_dev, &ppa_list[i]);
  385. ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
  386. }
  387. }
  388. static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
  389. struct ppa_addr *ppa_list, int nr_ppas)
  390. {
  391. int i;
  392. for (i = 0; i < nr_ppas; i++) {
  393. ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
  394. nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
  395. }
  396. }
  397. static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  398. {
  399. if (rqd->nr_ppas == 1) {
  400. nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
  401. return;
  402. }
  403. nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
  404. }
  405. static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  406. {
  407. if (rqd->nr_ppas == 1) {
  408. nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
  409. return;
  410. }
  411. nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
  412. }
  413. void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
  414. int len)
  415. {
  416. struct nvm_geo *geo = &dev->geo;
  417. struct nvm_dev_map *dev_rmap = dev->rmap;
  418. u64 i;
  419. for (i = 0; i < len; i++) {
  420. struct nvm_ch_map *ch_rmap;
  421. int *lun_roffs;
  422. struct ppa_addr gaddr;
  423. u64 pba = le64_to_cpu(entries[i]);
  424. u64 diff;
  425. if (!pba)
  426. continue;
  427. gaddr = linear_to_generic_addr(geo, pba);
  428. ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
  429. lun_roffs = ch_rmap->lun_offs;
  430. diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
  431. (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
  432. entries[i] -= cpu_to_le64(diff);
  433. }
  434. }
  435. EXPORT_SYMBOL(nvm_part_to_tgt);
  436. struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
  437. {
  438. struct nvm_tgt_type *tmp, *tt = NULL;
  439. if (lock)
  440. down_write(&nvm_tgtt_lock);
  441. list_for_each_entry(tmp, &nvm_tgt_types, list)
  442. if (!strcmp(name, tmp->name)) {
  443. tt = tmp;
  444. break;
  445. }
  446. if (lock)
  447. up_write(&nvm_tgtt_lock);
  448. return tt;
  449. }
  450. EXPORT_SYMBOL(nvm_find_target_type);
  451. int nvm_register_tgt_type(struct nvm_tgt_type *tt)
  452. {
  453. int ret = 0;
  454. down_write(&nvm_tgtt_lock);
  455. if (nvm_find_target_type(tt->name, 0))
  456. ret = -EEXIST;
  457. else
  458. list_add(&tt->list, &nvm_tgt_types);
  459. up_write(&nvm_tgtt_lock);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL(nvm_register_tgt_type);
  463. void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
  464. {
  465. if (!tt)
  466. return;
  467. down_write(&nvm_lock);
  468. list_del(&tt->list);
  469. up_write(&nvm_lock);
  470. }
  471. EXPORT_SYMBOL(nvm_unregister_tgt_type);
  472. void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
  473. dma_addr_t *dma_handler)
  474. {
  475. return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
  476. dma_handler);
  477. }
  478. EXPORT_SYMBOL(nvm_dev_dma_alloc);
  479. void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
  480. {
  481. dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
  482. }
  483. EXPORT_SYMBOL(nvm_dev_dma_free);
  484. static struct nvm_dev *nvm_find_nvm_dev(const char *name)
  485. {
  486. struct nvm_dev *dev;
  487. list_for_each_entry(dev, &nvm_devices, devices)
  488. if (!strcmp(name, dev->name))
  489. return dev;
  490. return NULL;
  491. }
  492. int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
  493. int nr_ppas, int type)
  494. {
  495. struct nvm_dev *dev = tgt_dev->parent;
  496. struct nvm_rq rqd;
  497. int ret;
  498. if (nr_ppas > dev->ops->max_phys_sect) {
  499. pr_err("nvm: unable to update all blocks atomically\n");
  500. return -EINVAL;
  501. }
  502. memset(&rqd, 0, sizeof(struct nvm_rq));
  503. nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
  504. nvm_rq_tgt_to_dev(tgt_dev, &rqd);
  505. ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
  506. nvm_free_rqd_ppalist(tgt_dev, &rqd);
  507. if (ret) {
  508. pr_err("nvm: failed bb mark\n");
  509. return -EINVAL;
  510. }
  511. return 0;
  512. }
  513. EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
  514. int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
  515. {
  516. struct nvm_dev *dev = tgt_dev->parent;
  517. return dev->ops->max_phys_sect;
  518. }
  519. EXPORT_SYMBOL(nvm_max_phys_sects);
  520. int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  521. {
  522. struct nvm_dev *dev = tgt_dev->parent;
  523. int ret;
  524. if (!dev->ops->submit_io)
  525. return -ENODEV;
  526. nvm_rq_tgt_to_dev(tgt_dev, rqd);
  527. rqd->dev = tgt_dev;
  528. /* In case of error, fail with right address format */
  529. ret = dev->ops->submit_io(dev, rqd);
  530. if (ret)
  531. nvm_rq_dev_to_tgt(tgt_dev, rqd);
  532. return ret;
  533. }
  534. EXPORT_SYMBOL(nvm_submit_io);
  535. static void nvm_end_io_sync(struct nvm_rq *rqd)
  536. {
  537. struct completion *waiting = rqd->private;
  538. complete(waiting);
  539. }
  540. int nvm_erase_sync(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
  541. int nr_ppas)
  542. {
  543. struct nvm_geo *geo = &tgt_dev->geo;
  544. struct nvm_rq rqd;
  545. int ret;
  546. DECLARE_COMPLETION_ONSTACK(wait);
  547. memset(&rqd, 0, sizeof(struct nvm_rq));
  548. rqd.opcode = NVM_OP_ERASE;
  549. rqd.end_io = nvm_end_io_sync;
  550. rqd.private = &wait;
  551. rqd.flags = geo->plane_mode >> 1;
  552. ret = nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
  553. if (ret)
  554. return ret;
  555. ret = nvm_submit_io(tgt_dev, &rqd);
  556. if (ret) {
  557. pr_err("rrpr: erase I/O submission failed: %d\n", ret);
  558. goto free_ppa_list;
  559. }
  560. wait_for_completion_io(&wait);
  561. free_ppa_list:
  562. nvm_free_rqd_ppalist(tgt_dev, &rqd);
  563. return ret;
  564. }
  565. EXPORT_SYMBOL(nvm_erase_sync);
  566. int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
  567. nvm_l2p_update_fn *update_l2p, void *priv)
  568. {
  569. struct nvm_dev *dev = tgt_dev->parent;
  570. if (!dev->ops->get_l2p_tbl)
  571. return 0;
  572. return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
  573. }
  574. EXPORT_SYMBOL(nvm_get_l2p_tbl);
  575. int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
  576. {
  577. struct nvm_dev *dev = tgt_dev->parent;
  578. struct nvm_geo *geo = &dev->geo;
  579. struct nvm_area *area, *prev, *next;
  580. sector_t begin = 0;
  581. sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
  582. if (len > max_sectors)
  583. return -EINVAL;
  584. area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
  585. if (!area)
  586. return -ENOMEM;
  587. prev = NULL;
  588. spin_lock(&dev->lock);
  589. list_for_each_entry(next, &dev->area_list, list) {
  590. if (begin + len > next->begin) {
  591. begin = next->end;
  592. prev = next;
  593. continue;
  594. }
  595. break;
  596. }
  597. if ((begin + len) > max_sectors) {
  598. spin_unlock(&dev->lock);
  599. kfree(area);
  600. return -EINVAL;
  601. }
  602. area->begin = *lba = begin;
  603. area->end = begin + len;
  604. if (prev) /* insert into sorted order */
  605. list_add(&area->list, &prev->list);
  606. else
  607. list_add(&area->list, &dev->area_list);
  608. spin_unlock(&dev->lock);
  609. return 0;
  610. }
  611. EXPORT_SYMBOL(nvm_get_area);
  612. void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
  613. {
  614. struct nvm_dev *dev = tgt_dev->parent;
  615. struct nvm_area *area;
  616. spin_lock(&dev->lock);
  617. list_for_each_entry(area, &dev->area_list, list) {
  618. if (area->begin != begin)
  619. continue;
  620. list_del(&area->list);
  621. spin_unlock(&dev->lock);
  622. kfree(area);
  623. return;
  624. }
  625. spin_unlock(&dev->lock);
  626. }
  627. EXPORT_SYMBOL(nvm_put_area);
  628. int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
  629. const struct ppa_addr *ppas, int nr_ppas, int vblk)
  630. {
  631. struct nvm_dev *dev = tgt_dev->parent;
  632. struct nvm_geo *geo = &tgt_dev->geo;
  633. int i, plane_cnt, pl_idx;
  634. struct ppa_addr ppa;
  635. if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
  636. rqd->nr_ppas = nr_ppas;
  637. rqd->ppa_addr = ppas[0];
  638. return 0;
  639. }
  640. rqd->nr_ppas = nr_ppas;
  641. rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
  642. if (!rqd->ppa_list) {
  643. pr_err("nvm: failed to allocate dma memory\n");
  644. return -ENOMEM;
  645. }
  646. if (!vblk) {
  647. for (i = 0; i < nr_ppas; i++)
  648. rqd->ppa_list[i] = ppas[i];
  649. } else {
  650. plane_cnt = geo->plane_mode;
  651. rqd->nr_ppas *= plane_cnt;
  652. for (i = 0; i < nr_ppas; i++) {
  653. for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
  654. ppa = ppas[i];
  655. ppa.g.pl = pl_idx;
  656. rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
  657. }
  658. }
  659. }
  660. return 0;
  661. }
  662. EXPORT_SYMBOL(nvm_set_rqd_ppalist);
  663. void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
  664. {
  665. if (!rqd->ppa_list)
  666. return;
  667. nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
  668. }
  669. EXPORT_SYMBOL(nvm_free_rqd_ppalist);
  670. void nvm_end_io(struct nvm_rq *rqd)
  671. {
  672. struct nvm_tgt_dev *tgt_dev = rqd->dev;
  673. /* Convert address space */
  674. if (tgt_dev)
  675. nvm_rq_dev_to_tgt(tgt_dev, rqd);
  676. if (rqd->end_io)
  677. rqd->end_io(rqd);
  678. }
  679. EXPORT_SYMBOL(nvm_end_io);
  680. /*
  681. * folds a bad block list from its plane representation to its virtual
  682. * block representation. The fold is done in place and reduced size is
  683. * returned.
  684. *
  685. * If any of the planes status are bad or grown bad block, the virtual block
  686. * is marked bad. If not bad, the first plane state acts as the block state.
  687. */
  688. int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
  689. {
  690. struct nvm_geo *geo = &dev->geo;
  691. int blk, offset, pl, blktype;
  692. if (nr_blks != geo->blks_per_lun * geo->plane_mode)
  693. return -EINVAL;
  694. for (blk = 0; blk < geo->blks_per_lun; blk++) {
  695. offset = blk * geo->plane_mode;
  696. blktype = blks[offset];
  697. /* Bad blocks on any planes take precedence over other types */
  698. for (pl = 0; pl < geo->plane_mode; pl++) {
  699. if (blks[offset + pl] &
  700. (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
  701. blktype = blks[offset + pl];
  702. break;
  703. }
  704. }
  705. blks[blk] = blktype;
  706. }
  707. return geo->blks_per_lun;
  708. }
  709. EXPORT_SYMBOL(nvm_bb_tbl_fold);
  710. int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
  711. u8 *blks)
  712. {
  713. struct nvm_dev *dev = tgt_dev->parent;
  714. nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
  715. return dev->ops->get_bb_tbl(dev, ppa, blks);
  716. }
  717. EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
  718. static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
  719. {
  720. struct nvm_geo *geo = &dev->geo;
  721. int i;
  722. dev->lps_per_blk = geo->pgs_per_blk;
  723. dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
  724. if (!dev->lptbl)
  725. return -ENOMEM;
  726. /* Just a linear array */
  727. for (i = 0; i < dev->lps_per_blk; i++)
  728. dev->lptbl[i] = i;
  729. return 0;
  730. }
  731. static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
  732. {
  733. int i, p;
  734. struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
  735. if (!mlc->num_pairs)
  736. return 0;
  737. dev->lps_per_blk = mlc->num_pairs;
  738. dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
  739. if (!dev->lptbl)
  740. return -ENOMEM;
  741. /* The lower page table encoding consists of a list of bytes, where each
  742. * has a lower and an upper half. The first half byte maintains the
  743. * increment value and every value after is an offset added to the
  744. * previous incrementation value
  745. */
  746. dev->lptbl[0] = mlc->pairs[0] & 0xF;
  747. for (i = 1; i < dev->lps_per_blk; i++) {
  748. p = mlc->pairs[i >> 1];
  749. if (i & 0x1) /* upper */
  750. dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
  751. else /* lower */
  752. dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
  753. }
  754. return 0;
  755. }
  756. static int nvm_core_init(struct nvm_dev *dev)
  757. {
  758. struct nvm_id *id = &dev->identity;
  759. struct nvm_id_group *grp = &id->grp;
  760. struct nvm_geo *geo = &dev->geo;
  761. int ret;
  762. /* Whole device values */
  763. geo->nr_chnls = grp->num_ch;
  764. geo->luns_per_chnl = grp->num_lun;
  765. /* Generic device values */
  766. geo->pgs_per_blk = grp->num_pg;
  767. geo->blks_per_lun = grp->num_blk;
  768. geo->nr_planes = grp->num_pln;
  769. geo->fpg_size = grp->fpg_sz;
  770. geo->pfpg_size = grp->fpg_sz * grp->num_pln;
  771. geo->sec_size = grp->csecs;
  772. geo->oob_size = grp->sos;
  773. geo->sec_per_pg = grp->fpg_sz / grp->csecs;
  774. geo->mccap = grp->mccap;
  775. memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
  776. geo->plane_mode = NVM_PLANE_SINGLE;
  777. geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
  778. if (grp->mpos & 0x020202)
  779. geo->plane_mode = NVM_PLANE_DOUBLE;
  780. if (grp->mpos & 0x040404)
  781. geo->plane_mode = NVM_PLANE_QUAD;
  782. if (grp->mtype != 0) {
  783. pr_err("nvm: memory type not supported\n");
  784. return -EINVAL;
  785. }
  786. /* calculated values */
  787. geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
  788. geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
  789. geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
  790. geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
  791. dev->total_secs = geo->nr_luns * geo->sec_per_lun;
  792. dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
  793. sizeof(unsigned long), GFP_KERNEL);
  794. if (!dev->lun_map)
  795. return -ENOMEM;
  796. switch (grp->fmtype) {
  797. case NVM_ID_FMTYPE_SLC:
  798. if (nvm_init_slc_tbl(dev, grp)) {
  799. ret = -ENOMEM;
  800. goto err_fmtype;
  801. }
  802. break;
  803. case NVM_ID_FMTYPE_MLC:
  804. if (nvm_init_mlc_tbl(dev, grp)) {
  805. ret = -ENOMEM;
  806. goto err_fmtype;
  807. }
  808. break;
  809. default:
  810. pr_err("nvm: flash type not supported\n");
  811. ret = -EINVAL;
  812. goto err_fmtype;
  813. }
  814. INIT_LIST_HEAD(&dev->area_list);
  815. INIT_LIST_HEAD(&dev->targets);
  816. mutex_init(&dev->mlock);
  817. spin_lock_init(&dev->lock);
  818. ret = nvm_register_map(dev);
  819. if (ret)
  820. goto err_fmtype;
  821. blk_queue_logical_block_size(dev->q, geo->sec_size);
  822. return 0;
  823. err_fmtype:
  824. kfree(dev->lun_map);
  825. return ret;
  826. }
  827. static void nvm_free(struct nvm_dev *dev)
  828. {
  829. if (!dev)
  830. return;
  831. if (dev->dma_pool)
  832. dev->ops->destroy_dma_pool(dev->dma_pool);
  833. nvm_unregister_map(dev);
  834. kfree(dev->lptbl);
  835. kfree(dev->lun_map);
  836. kfree(dev);
  837. }
  838. static int nvm_init(struct nvm_dev *dev)
  839. {
  840. struct nvm_geo *geo = &dev->geo;
  841. int ret = -EINVAL;
  842. if (dev->ops->identity(dev, &dev->identity)) {
  843. pr_err("nvm: device could not be identified\n");
  844. goto err;
  845. }
  846. pr_debug("nvm: ver:%x nvm_vendor:%x\n",
  847. dev->identity.ver_id, dev->identity.vmnt);
  848. if (dev->identity.ver_id != 1) {
  849. pr_err("nvm: device not supported by kernel.");
  850. goto err;
  851. }
  852. ret = nvm_core_init(dev);
  853. if (ret) {
  854. pr_err("nvm: could not initialize core structures.\n");
  855. goto err;
  856. }
  857. pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
  858. dev->name, geo->sec_per_pg, geo->nr_planes,
  859. geo->pgs_per_blk, geo->blks_per_lun,
  860. geo->nr_luns, geo->nr_chnls);
  861. return 0;
  862. err:
  863. pr_err("nvm: failed to initialize nvm\n");
  864. return ret;
  865. }
  866. struct nvm_dev *nvm_alloc_dev(int node)
  867. {
  868. return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
  869. }
  870. EXPORT_SYMBOL(nvm_alloc_dev);
  871. int nvm_register(struct nvm_dev *dev)
  872. {
  873. int ret;
  874. if (!dev->q || !dev->ops)
  875. return -EINVAL;
  876. if (dev->ops->max_phys_sect > 256) {
  877. pr_info("nvm: max sectors supported is 256.\n");
  878. return -EINVAL;
  879. }
  880. if (dev->ops->max_phys_sect > 1) {
  881. dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
  882. if (!dev->dma_pool) {
  883. pr_err("nvm: could not create dma pool\n");
  884. return -ENOMEM;
  885. }
  886. }
  887. ret = nvm_init(dev);
  888. if (ret)
  889. goto err_init;
  890. /* register device with a supported media manager */
  891. down_write(&nvm_lock);
  892. list_add(&dev->devices, &nvm_devices);
  893. up_write(&nvm_lock);
  894. return 0;
  895. err_init:
  896. dev->ops->destroy_dma_pool(dev->dma_pool);
  897. return ret;
  898. }
  899. EXPORT_SYMBOL(nvm_register);
  900. void nvm_unregister(struct nvm_dev *dev)
  901. {
  902. struct nvm_target *t, *tmp;
  903. mutex_lock(&dev->mlock);
  904. list_for_each_entry_safe(t, tmp, &dev->targets, list) {
  905. if (t->dev->parent != dev)
  906. continue;
  907. __nvm_remove_target(t);
  908. }
  909. mutex_unlock(&dev->mlock);
  910. down_write(&nvm_lock);
  911. list_del(&dev->devices);
  912. up_write(&nvm_lock);
  913. nvm_free(dev);
  914. }
  915. EXPORT_SYMBOL(nvm_unregister);
  916. static int __nvm_configure_create(struct nvm_ioctl_create *create)
  917. {
  918. struct nvm_dev *dev;
  919. struct nvm_ioctl_create_simple *s;
  920. down_write(&nvm_lock);
  921. dev = nvm_find_nvm_dev(create->dev);
  922. up_write(&nvm_lock);
  923. if (!dev) {
  924. pr_err("nvm: device not found\n");
  925. return -EINVAL;
  926. }
  927. if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
  928. pr_err("nvm: config type not valid\n");
  929. return -EINVAL;
  930. }
  931. s = &create->conf.s;
  932. if (s->lun_begin == -1 && s->lun_end == -1) {
  933. s->lun_begin = 0;
  934. s->lun_end = dev->geo.nr_luns - 1;
  935. }
  936. if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.nr_luns) {
  937. pr_err("nvm: lun out of bound (%u:%u > %u)\n",
  938. s->lun_begin, s->lun_end, dev->geo.nr_luns - 1);
  939. return -EINVAL;
  940. }
  941. return nvm_create_tgt(dev, create);
  942. }
  943. static long nvm_ioctl_info(struct file *file, void __user *arg)
  944. {
  945. struct nvm_ioctl_info *info;
  946. struct nvm_tgt_type *tt;
  947. int tgt_iter = 0;
  948. if (!capable(CAP_SYS_ADMIN))
  949. return -EPERM;
  950. info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
  951. if (IS_ERR(info))
  952. return -EFAULT;
  953. info->version[0] = NVM_VERSION_MAJOR;
  954. info->version[1] = NVM_VERSION_MINOR;
  955. info->version[2] = NVM_VERSION_PATCH;
  956. down_write(&nvm_lock);
  957. list_for_each_entry(tt, &nvm_tgt_types, list) {
  958. struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
  959. tgt->version[0] = tt->version[0];
  960. tgt->version[1] = tt->version[1];
  961. tgt->version[2] = tt->version[2];
  962. strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
  963. tgt_iter++;
  964. }
  965. info->tgtsize = tgt_iter;
  966. up_write(&nvm_lock);
  967. if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
  968. kfree(info);
  969. return -EFAULT;
  970. }
  971. kfree(info);
  972. return 0;
  973. }
  974. static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
  975. {
  976. struct nvm_ioctl_get_devices *devices;
  977. struct nvm_dev *dev;
  978. int i = 0;
  979. if (!capable(CAP_SYS_ADMIN))
  980. return -EPERM;
  981. devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
  982. if (!devices)
  983. return -ENOMEM;
  984. down_write(&nvm_lock);
  985. list_for_each_entry(dev, &nvm_devices, devices) {
  986. struct nvm_ioctl_device_info *info = &devices->info[i];
  987. strlcpy(info->devname, dev->name, sizeof(info->devname));
  988. /* kept for compatibility */
  989. info->bmversion[0] = 1;
  990. info->bmversion[1] = 0;
  991. info->bmversion[2] = 0;
  992. strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
  993. i++;
  994. if (i > 31) {
  995. pr_err("nvm: max 31 devices can be reported.\n");
  996. break;
  997. }
  998. }
  999. up_write(&nvm_lock);
  1000. devices->nr_devices = i;
  1001. if (copy_to_user(arg, devices,
  1002. sizeof(struct nvm_ioctl_get_devices))) {
  1003. kfree(devices);
  1004. return -EFAULT;
  1005. }
  1006. kfree(devices);
  1007. return 0;
  1008. }
  1009. static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
  1010. {
  1011. struct nvm_ioctl_create create;
  1012. if (!capable(CAP_SYS_ADMIN))
  1013. return -EPERM;
  1014. if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
  1015. return -EFAULT;
  1016. create.dev[DISK_NAME_LEN - 1] = '\0';
  1017. create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
  1018. create.tgtname[DISK_NAME_LEN - 1] = '\0';
  1019. if (create.flags != 0) {
  1020. __u32 flags = create.flags;
  1021. /* Check for valid flags */
  1022. if (flags & NVM_TARGET_FACTORY)
  1023. flags &= ~NVM_TARGET_FACTORY;
  1024. if (flags) {
  1025. pr_err("nvm: flag not supported\n");
  1026. return -EINVAL;
  1027. }
  1028. }
  1029. return __nvm_configure_create(&create);
  1030. }
  1031. static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
  1032. {
  1033. struct nvm_ioctl_remove remove;
  1034. struct nvm_dev *dev;
  1035. int ret = 0;
  1036. if (!capable(CAP_SYS_ADMIN))
  1037. return -EPERM;
  1038. if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
  1039. return -EFAULT;
  1040. remove.tgtname[DISK_NAME_LEN - 1] = '\0';
  1041. if (remove.flags != 0) {
  1042. pr_err("nvm: no flags supported\n");
  1043. return -EINVAL;
  1044. }
  1045. list_for_each_entry(dev, &nvm_devices, devices) {
  1046. ret = nvm_remove_tgt(dev, &remove);
  1047. if (!ret)
  1048. break;
  1049. }
  1050. return ret;
  1051. }
  1052. /* kept for compatibility reasons */
  1053. static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
  1054. {
  1055. struct nvm_ioctl_dev_init init;
  1056. if (!capable(CAP_SYS_ADMIN))
  1057. return -EPERM;
  1058. if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
  1059. return -EFAULT;
  1060. if (init.flags != 0) {
  1061. pr_err("nvm: no flags supported\n");
  1062. return -EINVAL;
  1063. }
  1064. return 0;
  1065. }
  1066. /* Kept for compatibility reasons */
  1067. static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
  1068. {
  1069. struct nvm_ioctl_dev_factory fact;
  1070. if (!capable(CAP_SYS_ADMIN))
  1071. return -EPERM;
  1072. if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
  1073. return -EFAULT;
  1074. fact.dev[DISK_NAME_LEN - 1] = '\0';
  1075. if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
  1076. return -EINVAL;
  1077. return 0;
  1078. }
  1079. static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
  1080. {
  1081. void __user *argp = (void __user *)arg;
  1082. switch (cmd) {
  1083. case NVM_INFO:
  1084. return nvm_ioctl_info(file, argp);
  1085. case NVM_GET_DEVICES:
  1086. return nvm_ioctl_get_devices(file, argp);
  1087. case NVM_DEV_CREATE:
  1088. return nvm_ioctl_dev_create(file, argp);
  1089. case NVM_DEV_REMOVE:
  1090. return nvm_ioctl_dev_remove(file, argp);
  1091. case NVM_DEV_INIT:
  1092. return nvm_ioctl_dev_init(file, argp);
  1093. case NVM_DEV_FACTORY:
  1094. return nvm_ioctl_dev_factory(file, argp);
  1095. }
  1096. return 0;
  1097. }
  1098. static const struct file_operations _ctl_fops = {
  1099. .open = nonseekable_open,
  1100. .unlocked_ioctl = nvm_ctl_ioctl,
  1101. .owner = THIS_MODULE,
  1102. .llseek = noop_llseek,
  1103. };
  1104. static struct miscdevice _nvm_misc = {
  1105. .minor = MISC_DYNAMIC_MINOR,
  1106. .name = "lightnvm",
  1107. .nodename = "lightnvm/control",
  1108. .fops = &_ctl_fops,
  1109. };
  1110. builtin_misc_device(_nvm_misc);