lightnvm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * nvme-lightnvm.c - LightNVM NVMe device
  3. *
  4. * Copyright (C) 2014-2015 IT University of Copenhagen
  5. * Initial release: Matias Bjorling <mb@lightnvm.io>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; see the file COPYING. If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  19. * USA.
  20. *
  21. */
  22. #include "nvme.h"
  23. #include <linux/nvme.h>
  24. #include <linux/bitops.h>
  25. #include <linux/lightnvm.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/sched/sysctl.h>
  28. #include <uapi/linux/lightnvm.h>
  29. enum nvme_nvm_admin_opcode {
  30. nvme_nvm_admin_identity = 0xe2,
  31. nvme_nvm_admin_get_l2p_tbl = 0xea,
  32. nvme_nvm_admin_get_bb_tbl = 0xf2,
  33. nvme_nvm_admin_set_bb_tbl = 0xf1,
  34. };
  35. struct nvme_nvm_hb_rw {
  36. __u8 opcode;
  37. __u8 flags;
  38. __u16 command_id;
  39. __le32 nsid;
  40. __u64 rsvd2;
  41. __le64 metadata;
  42. __le64 prp1;
  43. __le64 prp2;
  44. __le64 spba;
  45. __le16 length;
  46. __le16 control;
  47. __le32 dsmgmt;
  48. __le64 slba;
  49. };
  50. struct nvme_nvm_ph_rw {
  51. __u8 opcode;
  52. __u8 flags;
  53. __u16 command_id;
  54. __le32 nsid;
  55. __u64 rsvd2;
  56. __le64 metadata;
  57. __le64 prp1;
  58. __le64 prp2;
  59. __le64 spba;
  60. __le16 length;
  61. __le16 control;
  62. __le32 dsmgmt;
  63. __le64 resv;
  64. };
  65. struct nvme_nvm_identity {
  66. __u8 opcode;
  67. __u8 flags;
  68. __u16 command_id;
  69. __le32 nsid;
  70. __u64 rsvd[2];
  71. __le64 prp1;
  72. __le64 prp2;
  73. __le32 chnl_off;
  74. __u32 rsvd11[5];
  75. };
  76. struct nvme_nvm_l2ptbl {
  77. __u8 opcode;
  78. __u8 flags;
  79. __u16 command_id;
  80. __le32 nsid;
  81. __le32 cdw2[4];
  82. __le64 prp1;
  83. __le64 prp2;
  84. __le64 slba;
  85. __le32 nlb;
  86. __le16 cdw14[6];
  87. };
  88. struct nvme_nvm_getbbtbl {
  89. __u8 opcode;
  90. __u8 flags;
  91. __u16 command_id;
  92. __le32 nsid;
  93. __u64 rsvd[2];
  94. __le64 prp1;
  95. __le64 prp2;
  96. __le64 spba;
  97. __u32 rsvd4[4];
  98. };
  99. struct nvme_nvm_setbbtbl {
  100. __u8 opcode;
  101. __u8 flags;
  102. __u16 command_id;
  103. __le32 nsid;
  104. __le64 rsvd[2];
  105. __le64 prp1;
  106. __le64 prp2;
  107. __le64 spba;
  108. __le16 nlb;
  109. __u8 value;
  110. __u8 rsvd3;
  111. __u32 rsvd4[3];
  112. };
  113. struct nvme_nvm_erase_blk {
  114. __u8 opcode;
  115. __u8 flags;
  116. __u16 command_id;
  117. __le32 nsid;
  118. __u64 rsvd[2];
  119. __le64 prp1;
  120. __le64 prp2;
  121. __le64 spba;
  122. __le16 length;
  123. __le16 control;
  124. __le32 dsmgmt;
  125. __le64 resv;
  126. };
  127. struct nvme_nvm_command {
  128. union {
  129. struct nvme_common_command common;
  130. struct nvme_nvm_identity identity;
  131. struct nvme_nvm_hb_rw hb_rw;
  132. struct nvme_nvm_ph_rw ph_rw;
  133. struct nvme_nvm_l2ptbl l2p;
  134. struct nvme_nvm_getbbtbl get_bb;
  135. struct nvme_nvm_setbbtbl set_bb;
  136. struct nvme_nvm_erase_blk erase;
  137. };
  138. };
  139. #define NVME_NVM_LP_MLC_PAIRS 886
  140. struct nvme_nvm_lp_mlc {
  141. __le16 num_pairs;
  142. __u8 pairs[NVME_NVM_LP_MLC_PAIRS];
  143. };
  144. struct nvme_nvm_lp_tbl {
  145. __u8 id[8];
  146. struct nvme_nvm_lp_mlc mlc;
  147. };
  148. struct nvme_nvm_id_group {
  149. __u8 mtype;
  150. __u8 fmtype;
  151. __le16 res16;
  152. __u8 num_ch;
  153. __u8 num_lun;
  154. __u8 num_pln;
  155. __u8 rsvd1;
  156. __le16 num_blk;
  157. __le16 num_pg;
  158. __le16 fpg_sz;
  159. __le16 csecs;
  160. __le16 sos;
  161. __le16 rsvd2;
  162. __le32 trdt;
  163. __le32 trdm;
  164. __le32 tprt;
  165. __le32 tprm;
  166. __le32 tbet;
  167. __le32 tbem;
  168. __le32 mpos;
  169. __le32 mccap;
  170. __le16 cpar;
  171. __u8 reserved[10];
  172. struct nvme_nvm_lp_tbl lptbl;
  173. } __packed;
  174. struct nvme_nvm_addr_format {
  175. __u8 ch_offset;
  176. __u8 ch_len;
  177. __u8 lun_offset;
  178. __u8 lun_len;
  179. __u8 pln_offset;
  180. __u8 pln_len;
  181. __u8 blk_offset;
  182. __u8 blk_len;
  183. __u8 pg_offset;
  184. __u8 pg_len;
  185. __u8 sect_offset;
  186. __u8 sect_len;
  187. __u8 res[4];
  188. } __packed;
  189. struct nvme_nvm_id {
  190. __u8 ver_id;
  191. __u8 vmnt;
  192. __u8 cgrps;
  193. __u8 res;
  194. __le32 cap;
  195. __le32 dom;
  196. struct nvme_nvm_addr_format ppaf;
  197. __u8 resv[228];
  198. struct nvme_nvm_id_group groups[4];
  199. } __packed;
  200. struct nvme_nvm_bb_tbl {
  201. __u8 tblid[4];
  202. __le16 verid;
  203. __le16 revid;
  204. __le32 rvsd1;
  205. __le32 tblks;
  206. __le32 tfact;
  207. __le32 tgrown;
  208. __le32 tdresv;
  209. __le32 thresv;
  210. __le32 rsvd2[8];
  211. __u8 blk[0];
  212. };
  213. /*
  214. * Check we didn't inadvertently grow the command struct
  215. */
  216. static inline void _nvme_nvm_check_size(void)
  217. {
  218. BUILD_BUG_ON(sizeof(struct nvme_nvm_identity) != 64);
  219. BUILD_BUG_ON(sizeof(struct nvme_nvm_hb_rw) != 64);
  220. BUILD_BUG_ON(sizeof(struct nvme_nvm_ph_rw) != 64);
  221. BUILD_BUG_ON(sizeof(struct nvme_nvm_getbbtbl) != 64);
  222. BUILD_BUG_ON(sizeof(struct nvme_nvm_setbbtbl) != 64);
  223. BUILD_BUG_ON(sizeof(struct nvme_nvm_l2ptbl) != 64);
  224. BUILD_BUG_ON(sizeof(struct nvme_nvm_erase_blk) != 64);
  225. BUILD_BUG_ON(sizeof(struct nvme_nvm_id_group) != 960);
  226. BUILD_BUG_ON(sizeof(struct nvme_nvm_addr_format) != 16);
  227. BUILD_BUG_ON(sizeof(struct nvme_nvm_id) != NVME_IDENTIFY_DATA_SIZE);
  228. BUILD_BUG_ON(sizeof(struct nvme_nvm_bb_tbl) != 64);
  229. }
  230. static int init_grps(struct nvm_id *nvm_id, struct nvme_nvm_id *nvme_nvm_id)
  231. {
  232. struct nvme_nvm_id_group *src;
  233. struct nvm_id_group *dst;
  234. if (nvme_nvm_id->cgrps != 1)
  235. return -EINVAL;
  236. src = &nvme_nvm_id->groups[0];
  237. dst = &nvm_id->grp;
  238. dst->mtype = src->mtype;
  239. dst->fmtype = src->fmtype;
  240. dst->num_ch = src->num_ch;
  241. dst->num_lun = src->num_lun;
  242. dst->num_pln = src->num_pln;
  243. dst->num_pg = le16_to_cpu(src->num_pg);
  244. dst->num_blk = le16_to_cpu(src->num_blk);
  245. dst->fpg_sz = le16_to_cpu(src->fpg_sz);
  246. dst->csecs = le16_to_cpu(src->csecs);
  247. dst->sos = le16_to_cpu(src->sos);
  248. dst->trdt = le32_to_cpu(src->trdt);
  249. dst->trdm = le32_to_cpu(src->trdm);
  250. dst->tprt = le32_to_cpu(src->tprt);
  251. dst->tprm = le32_to_cpu(src->tprm);
  252. dst->tbet = le32_to_cpu(src->tbet);
  253. dst->tbem = le32_to_cpu(src->tbem);
  254. dst->mpos = le32_to_cpu(src->mpos);
  255. dst->mccap = le32_to_cpu(src->mccap);
  256. dst->cpar = le16_to_cpu(src->cpar);
  257. if (dst->fmtype == NVM_ID_FMTYPE_MLC) {
  258. memcpy(dst->lptbl.id, src->lptbl.id, 8);
  259. dst->lptbl.mlc.num_pairs =
  260. le16_to_cpu(src->lptbl.mlc.num_pairs);
  261. if (dst->lptbl.mlc.num_pairs > NVME_NVM_LP_MLC_PAIRS) {
  262. pr_err("nvm: number of MLC pairs not supported\n");
  263. return -EINVAL;
  264. }
  265. memcpy(dst->lptbl.mlc.pairs, src->lptbl.mlc.pairs,
  266. dst->lptbl.mlc.num_pairs);
  267. }
  268. return 0;
  269. }
  270. static int nvme_nvm_identity(struct nvm_dev *nvmdev, struct nvm_id *nvm_id)
  271. {
  272. struct nvme_ns *ns = nvmdev->q->queuedata;
  273. struct nvme_nvm_id *nvme_nvm_id;
  274. struct nvme_nvm_command c = {};
  275. int ret;
  276. c.identity.opcode = nvme_nvm_admin_identity;
  277. c.identity.nsid = cpu_to_le32(ns->ns_id);
  278. c.identity.chnl_off = 0;
  279. nvme_nvm_id = kmalloc(sizeof(struct nvme_nvm_id), GFP_KERNEL);
  280. if (!nvme_nvm_id)
  281. return -ENOMEM;
  282. ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c,
  283. nvme_nvm_id, sizeof(struct nvme_nvm_id));
  284. if (ret) {
  285. ret = -EIO;
  286. goto out;
  287. }
  288. nvm_id->ver_id = nvme_nvm_id->ver_id;
  289. nvm_id->vmnt = nvme_nvm_id->vmnt;
  290. nvm_id->cap = le32_to_cpu(nvme_nvm_id->cap);
  291. nvm_id->dom = le32_to_cpu(nvme_nvm_id->dom);
  292. memcpy(&nvm_id->ppaf, &nvme_nvm_id->ppaf,
  293. sizeof(struct nvm_addr_format));
  294. ret = init_grps(nvm_id, nvme_nvm_id);
  295. out:
  296. kfree(nvme_nvm_id);
  297. return ret;
  298. }
  299. static int nvme_nvm_get_l2p_tbl(struct nvm_dev *nvmdev, u64 slba, u32 nlb,
  300. nvm_l2p_update_fn *update_l2p, void *priv)
  301. {
  302. struct nvme_ns *ns = nvmdev->q->queuedata;
  303. struct nvme_nvm_command c = {};
  304. u32 len = queue_max_hw_sectors(ns->ctrl->admin_q) << 9;
  305. u32 nlb_pr_rq = len / sizeof(u64);
  306. u64 cmd_slba = slba;
  307. void *entries;
  308. int ret = 0;
  309. c.l2p.opcode = nvme_nvm_admin_get_l2p_tbl;
  310. c.l2p.nsid = cpu_to_le32(ns->ns_id);
  311. entries = kmalloc(len, GFP_KERNEL);
  312. if (!entries)
  313. return -ENOMEM;
  314. while (nlb) {
  315. u32 cmd_nlb = min(nlb_pr_rq, nlb);
  316. u64 elba = slba + cmd_nlb;
  317. c.l2p.slba = cpu_to_le64(cmd_slba);
  318. c.l2p.nlb = cpu_to_le32(cmd_nlb);
  319. ret = nvme_submit_sync_cmd(ns->ctrl->admin_q,
  320. (struct nvme_command *)&c, entries, len);
  321. if (ret) {
  322. dev_err(ns->ctrl->device,
  323. "L2P table transfer failed (%d)\n", ret);
  324. ret = -EIO;
  325. goto out;
  326. }
  327. if (unlikely(elba > nvmdev->total_secs)) {
  328. pr_err("nvm: L2P data from device is out of bounds!\n");
  329. ret = -EINVAL;
  330. goto out;
  331. }
  332. /* Transform physical address to target address space */
  333. nvm_part_to_tgt(nvmdev, entries, cmd_nlb);
  334. if (update_l2p(cmd_slba, cmd_nlb, entries, priv)) {
  335. ret = -EINTR;
  336. goto out;
  337. }
  338. cmd_slba += cmd_nlb;
  339. nlb -= cmd_nlb;
  340. }
  341. out:
  342. kfree(entries);
  343. return ret;
  344. }
  345. static int nvme_nvm_get_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr ppa,
  346. u8 *blks)
  347. {
  348. struct request_queue *q = nvmdev->q;
  349. struct nvm_geo *geo = &nvmdev->geo;
  350. struct nvme_ns *ns = q->queuedata;
  351. struct nvme_ctrl *ctrl = ns->ctrl;
  352. struct nvme_nvm_command c = {};
  353. struct nvme_nvm_bb_tbl *bb_tbl;
  354. int nr_blks = geo->blks_per_lun * geo->plane_mode;
  355. int tblsz = sizeof(struct nvme_nvm_bb_tbl) + nr_blks;
  356. int ret = 0;
  357. c.get_bb.opcode = nvme_nvm_admin_get_bb_tbl;
  358. c.get_bb.nsid = cpu_to_le32(ns->ns_id);
  359. c.get_bb.spba = cpu_to_le64(ppa.ppa);
  360. bb_tbl = kzalloc(tblsz, GFP_KERNEL);
  361. if (!bb_tbl)
  362. return -ENOMEM;
  363. ret = nvme_submit_sync_cmd(ctrl->admin_q, (struct nvme_command *)&c,
  364. bb_tbl, tblsz);
  365. if (ret) {
  366. dev_err(ctrl->device, "get bad block table failed (%d)\n", ret);
  367. ret = -EIO;
  368. goto out;
  369. }
  370. if (bb_tbl->tblid[0] != 'B' || bb_tbl->tblid[1] != 'B' ||
  371. bb_tbl->tblid[2] != 'L' || bb_tbl->tblid[3] != 'T') {
  372. dev_err(ctrl->device, "bbt format mismatch\n");
  373. ret = -EINVAL;
  374. goto out;
  375. }
  376. if (le16_to_cpu(bb_tbl->verid) != 1) {
  377. ret = -EINVAL;
  378. dev_err(ctrl->device, "bbt version not supported\n");
  379. goto out;
  380. }
  381. if (le32_to_cpu(bb_tbl->tblks) != nr_blks) {
  382. ret = -EINVAL;
  383. dev_err(ctrl->device,
  384. "bbt unsuspected blocks returned (%u!=%u)",
  385. le32_to_cpu(bb_tbl->tblks), nr_blks);
  386. goto out;
  387. }
  388. memcpy(blks, bb_tbl->blk, geo->blks_per_lun * geo->plane_mode);
  389. out:
  390. kfree(bb_tbl);
  391. return ret;
  392. }
  393. static int nvme_nvm_set_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr *ppas,
  394. int nr_ppas, int type)
  395. {
  396. struct nvme_ns *ns = nvmdev->q->queuedata;
  397. struct nvme_nvm_command c = {};
  398. int ret = 0;
  399. c.set_bb.opcode = nvme_nvm_admin_set_bb_tbl;
  400. c.set_bb.nsid = cpu_to_le32(ns->ns_id);
  401. c.set_bb.spba = cpu_to_le64(ppas->ppa);
  402. c.set_bb.nlb = cpu_to_le16(nr_ppas - 1);
  403. c.set_bb.value = type;
  404. ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c,
  405. NULL, 0);
  406. if (ret)
  407. dev_err(ns->ctrl->device, "set bad block table failed (%d)\n",
  408. ret);
  409. return ret;
  410. }
  411. static inline void nvme_nvm_rqtocmd(struct nvm_rq *rqd, struct nvme_ns *ns,
  412. struct nvme_nvm_command *c)
  413. {
  414. c->ph_rw.opcode = rqd->opcode;
  415. c->ph_rw.nsid = cpu_to_le32(ns->ns_id);
  416. c->ph_rw.spba = cpu_to_le64(rqd->ppa_addr.ppa);
  417. c->ph_rw.metadata = cpu_to_le64(rqd->dma_meta_list);
  418. c->ph_rw.control = cpu_to_le16(rqd->flags);
  419. c->ph_rw.length = cpu_to_le16(rqd->nr_ppas - 1);
  420. if (rqd->opcode == NVM_OP_HBWRITE || rqd->opcode == NVM_OP_HBREAD)
  421. c->hb_rw.slba = cpu_to_le64(nvme_block_nr(ns,
  422. rqd->bio->bi_iter.bi_sector));
  423. }
  424. static void nvme_nvm_end_io(struct request *rq, blk_status_t status)
  425. {
  426. struct nvm_rq *rqd = rq->end_io_data;
  427. rqd->ppa_status = le64_to_cpu(nvme_req(rq)->result.u64);
  428. rqd->error = nvme_req(rq)->status;
  429. nvm_end_io(rqd);
  430. kfree(nvme_req(rq)->cmd);
  431. blk_mq_free_request(rq);
  432. }
  433. static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  434. {
  435. struct request_queue *q = dev->q;
  436. struct nvme_ns *ns = q->queuedata;
  437. struct request *rq;
  438. struct bio *bio = rqd->bio;
  439. struct nvme_nvm_command *cmd;
  440. cmd = kzalloc(sizeof(struct nvme_nvm_command), GFP_KERNEL);
  441. if (!cmd)
  442. return -ENOMEM;
  443. nvme_nvm_rqtocmd(rqd, ns, cmd);
  444. rq = nvme_alloc_request(q, (struct nvme_command *)cmd, 0, NVME_QID_ANY);
  445. if (IS_ERR(rq)) {
  446. kfree(cmd);
  447. return PTR_ERR(rq);
  448. }
  449. rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
  450. if (bio) {
  451. blk_init_request_from_bio(rq, bio);
  452. } else {
  453. rq->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  454. rq->__data_len = 0;
  455. }
  456. rq->end_io_data = rqd;
  457. blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io);
  458. return 0;
  459. }
  460. static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name)
  461. {
  462. struct nvme_ns *ns = nvmdev->q->queuedata;
  463. return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0);
  464. }
  465. static void nvme_nvm_destroy_dma_pool(void *pool)
  466. {
  467. struct dma_pool *dma_pool = pool;
  468. dma_pool_destroy(dma_pool);
  469. }
  470. static void *nvme_nvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  471. gfp_t mem_flags, dma_addr_t *dma_handler)
  472. {
  473. return dma_pool_alloc(pool, mem_flags, dma_handler);
  474. }
  475. static void nvme_nvm_dev_dma_free(void *pool, void *addr,
  476. dma_addr_t dma_handler)
  477. {
  478. dma_pool_free(pool, addr, dma_handler);
  479. }
  480. static struct nvm_dev_ops nvme_nvm_dev_ops = {
  481. .identity = nvme_nvm_identity,
  482. .get_l2p_tbl = nvme_nvm_get_l2p_tbl,
  483. .get_bb_tbl = nvme_nvm_get_bb_tbl,
  484. .set_bb_tbl = nvme_nvm_set_bb_tbl,
  485. .submit_io = nvme_nvm_submit_io,
  486. .create_dma_pool = nvme_nvm_create_dma_pool,
  487. .destroy_dma_pool = nvme_nvm_destroy_dma_pool,
  488. .dev_dma_alloc = nvme_nvm_dev_dma_alloc,
  489. .dev_dma_free = nvme_nvm_dev_dma_free,
  490. .max_phys_sect = 64,
  491. };
  492. static int nvme_nvm_submit_user_cmd(struct request_queue *q,
  493. struct nvme_ns *ns,
  494. struct nvme_nvm_command *vcmd,
  495. void __user *ubuf, unsigned int bufflen,
  496. void __user *meta_buf, unsigned int meta_len,
  497. void __user *ppa_buf, unsigned int ppa_len,
  498. u32 *result, u64 *status, unsigned int timeout)
  499. {
  500. bool write = nvme_is_write((struct nvme_command *)vcmd);
  501. struct nvm_dev *dev = ns->ndev;
  502. struct gendisk *disk = ns->disk;
  503. struct request *rq;
  504. struct bio *bio = NULL;
  505. __le64 *ppa_list = NULL;
  506. dma_addr_t ppa_dma;
  507. __le64 *metadata = NULL;
  508. dma_addr_t metadata_dma;
  509. DECLARE_COMPLETION_ONSTACK(wait);
  510. int ret = 0;
  511. rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0,
  512. NVME_QID_ANY);
  513. if (IS_ERR(rq)) {
  514. ret = -ENOMEM;
  515. goto err_cmd;
  516. }
  517. rq->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  518. rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
  519. if (ppa_buf && ppa_len) {
  520. ppa_list = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, &ppa_dma);
  521. if (!ppa_list) {
  522. ret = -ENOMEM;
  523. goto err_rq;
  524. }
  525. if (copy_from_user(ppa_list, (void __user *)ppa_buf,
  526. sizeof(u64) * (ppa_len + 1))) {
  527. ret = -EFAULT;
  528. goto err_ppa;
  529. }
  530. vcmd->ph_rw.spba = cpu_to_le64(ppa_dma);
  531. } else {
  532. vcmd->ph_rw.spba = cpu_to_le64((uintptr_t)ppa_buf);
  533. }
  534. if (ubuf && bufflen) {
  535. ret = blk_rq_map_user(q, rq, NULL, ubuf, bufflen, GFP_KERNEL);
  536. if (ret)
  537. goto err_ppa;
  538. bio = rq->bio;
  539. if (meta_buf && meta_len) {
  540. metadata = dma_pool_alloc(dev->dma_pool, GFP_KERNEL,
  541. &metadata_dma);
  542. if (!metadata) {
  543. ret = -ENOMEM;
  544. goto err_map;
  545. }
  546. if (write) {
  547. if (copy_from_user(metadata,
  548. (void __user *)meta_buf,
  549. meta_len)) {
  550. ret = -EFAULT;
  551. goto err_meta;
  552. }
  553. }
  554. vcmd->ph_rw.metadata = cpu_to_le64(metadata_dma);
  555. }
  556. bio->bi_disk = disk;
  557. }
  558. blk_execute_rq(q, NULL, rq, 0);
  559. if (nvme_req(rq)->flags & NVME_REQ_CANCELLED)
  560. ret = -EINTR;
  561. else if (nvme_req(rq)->status & 0x7ff)
  562. ret = -EIO;
  563. if (result)
  564. *result = nvme_req(rq)->status & 0x7ff;
  565. if (status)
  566. *status = le64_to_cpu(nvme_req(rq)->result.u64);
  567. if (metadata && !ret && !write) {
  568. if (copy_to_user(meta_buf, (void *)metadata, meta_len))
  569. ret = -EFAULT;
  570. }
  571. err_meta:
  572. if (meta_buf && meta_len)
  573. dma_pool_free(dev->dma_pool, metadata, metadata_dma);
  574. err_map:
  575. if (bio)
  576. blk_rq_unmap_user(bio);
  577. err_ppa:
  578. if (ppa_buf && ppa_len)
  579. dma_pool_free(dev->dma_pool, ppa_list, ppa_dma);
  580. err_rq:
  581. blk_mq_free_request(rq);
  582. err_cmd:
  583. return ret;
  584. }
  585. static int nvme_nvm_submit_vio(struct nvme_ns *ns,
  586. struct nvm_user_vio __user *uvio)
  587. {
  588. struct nvm_user_vio vio;
  589. struct nvme_nvm_command c;
  590. unsigned int length;
  591. int ret;
  592. if (copy_from_user(&vio, uvio, sizeof(vio)))
  593. return -EFAULT;
  594. if (vio.flags)
  595. return -EINVAL;
  596. memset(&c, 0, sizeof(c));
  597. c.ph_rw.opcode = vio.opcode;
  598. c.ph_rw.nsid = cpu_to_le32(ns->ns_id);
  599. c.ph_rw.control = cpu_to_le16(vio.control);
  600. c.ph_rw.length = cpu_to_le16(vio.nppas);
  601. length = (vio.nppas + 1) << ns->lba_shift;
  602. ret = nvme_nvm_submit_user_cmd(ns->queue, ns, &c,
  603. (void __user *)(uintptr_t)vio.addr, length,
  604. (void __user *)(uintptr_t)vio.metadata,
  605. vio.metadata_len,
  606. (void __user *)(uintptr_t)vio.ppa_list, vio.nppas,
  607. &vio.result, &vio.status, 0);
  608. if (ret && copy_to_user(uvio, &vio, sizeof(vio)))
  609. return -EFAULT;
  610. return ret;
  611. }
  612. static int nvme_nvm_user_vcmd(struct nvme_ns *ns, int admin,
  613. struct nvm_passthru_vio __user *uvcmd)
  614. {
  615. struct nvm_passthru_vio vcmd;
  616. struct nvme_nvm_command c;
  617. struct request_queue *q;
  618. unsigned int timeout = 0;
  619. int ret;
  620. if (copy_from_user(&vcmd, uvcmd, sizeof(vcmd)))
  621. return -EFAULT;
  622. if ((vcmd.opcode != 0xF2) && (!capable(CAP_SYS_ADMIN)))
  623. return -EACCES;
  624. if (vcmd.flags)
  625. return -EINVAL;
  626. memset(&c, 0, sizeof(c));
  627. c.common.opcode = vcmd.opcode;
  628. c.common.nsid = cpu_to_le32(ns->ns_id);
  629. c.common.cdw2[0] = cpu_to_le32(vcmd.cdw2);
  630. c.common.cdw2[1] = cpu_to_le32(vcmd.cdw3);
  631. /* cdw11-12 */
  632. c.ph_rw.length = cpu_to_le16(vcmd.nppas);
  633. c.ph_rw.control = cpu_to_le16(vcmd.control);
  634. c.common.cdw10[3] = cpu_to_le32(vcmd.cdw13);
  635. c.common.cdw10[4] = cpu_to_le32(vcmd.cdw14);
  636. c.common.cdw10[5] = cpu_to_le32(vcmd.cdw15);
  637. if (vcmd.timeout_ms)
  638. timeout = msecs_to_jiffies(vcmd.timeout_ms);
  639. q = admin ? ns->ctrl->admin_q : ns->queue;
  640. ret = nvme_nvm_submit_user_cmd(q, ns,
  641. (struct nvme_nvm_command *)&c,
  642. (void __user *)(uintptr_t)vcmd.addr, vcmd.data_len,
  643. (void __user *)(uintptr_t)vcmd.metadata,
  644. vcmd.metadata_len,
  645. (void __user *)(uintptr_t)vcmd.ppa_list, vcmd.nppas,
  646. &vcmd.result, &vcmd.status, timeout);
  647. if (ret && copy_to_user(uvcmd, &vcmd, sizeof(vcmd)))
  648. return -EFAULT;
  649. return ret;
  650. }
  651. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg)
  652. {
  653. switch (cmd) {
  654. case NVME_NVM_IOCTL_ADMIN_VIO:
  655. return nvme_nvm_user_vcmd(ns, 1, (void __user *)arg);
  656. case NVME_NVM_IOCTL_IO_VIO:
  657. return nvme_nvm_user_vcmd(ns, 0, (void __user *)arg);
  658. case NVME_NVM_IOCTL_SUBMIT_VIO:
  659. return nvme_nvm_submit_vio(ns, (void __user *)arg);
  660. default:
  661. return -ENOTTY;
  662. }
  663. }
  664. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node)
  665. {
  666. struct request_queue *q = ns->queue;
  667. struct nvm_dev *dev;
  668. _nvme_nvm_check_size();
  669. dev = nvm_alloc_dev(node);
  670. if (!dev)
  671. return -ENOMEM;
  672. dev->q = q;
  673. memcpy(dev->name, disk_name, DISK_NAME_LEN);
  674. dev->ops = &nvme_nvm_dev_ops;
  675. dev->private_data = ns;
  676. ns->ndev = dev;
  677. return nvm_register(dev);
  678. }
  679. void nvme_nvm_unregister(struct nvme_ns *ns)
  680. {
  681. nvm_unregister(ns->ndev);
  682. }
  683. static ssize_t nvm_dev_attr_show(struct device *dev,
  684. struct device_attribute *dattr, char *page)
  685. {
  686. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  687. struct nvm_dev *ndev = ns->ndev;
  688. struct nvm_id *id;
  689. struct nvm_id_group *grp;
  690. struct attribute *attr;
  691. if (!ndev)
  692. return 0;
  693. id = &ndev->identity;
  694. grp = &id->grp;
  695. attr = &dattr->attr;
  696. if (strcmp(attr->name, "version") == 0) {
  697. return scnprintf(page, PAGE_SIZE, "%u\n", id->ver_id);
  698. } else if (strcmp(attr->name, "vendor_opcode") == 0) {
  699. return scnprintf(page, PAGE_SIZE, "%u\n", id->vmnt);
  700. } else if (strcmp(attr->name, "capabilities") == 0) {
  701. return scnprintf(page, PAGE_SIZE, "%u\n", id->cap);
  702. } else if (strcmp(attr->name, "device_mode") == 0) {
  703. return scnprintf(page, PAGE_SIZE, "%u\n", id->dom);
  704. /* kept for compatibility */
  705. } else if (strcmp(attr->name, "media_manager") == 0) {
  706. return scnprintf(page, PAGE_SIZE, "%s\n", "gennvm");
  707. } else if (strcmp(attr->name, "ppa_format") == 0) {
  708. return scnprintf(page, PAGE_SIZE,
  709. "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  710. id->ppaf.ch_offset, id->ppaf.ch_len,
  711. id->ppaf.lun_offset, id->ppaf.lun_len,
  712. id->ppaf.pln_offset, id->ppaf.pln_len,
  713. id->ppaf.blk_offset, id->ppaf.blk_len,
  714. id->ppaf.pg_offset, id->ppaf.pg_len,
  715. id->ppaf.sect_offset, id->ppaf.sect_len);
  716. } else if (strcmp(attr->name, "media_type") == 0) { /* u8 */
  717. return scnprintf(page, PAGE_SIZE, "%u\n", grp->mtype);
  718. } else if (strcmp(attr->name, "flash_media_type") == 0) {
  719. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fmtype);
  720. } else if (strcmp(attr->name, "num_channels") == 0) {
  721. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_ch);
  722. } else if (strcmp(attr->name, "num_luns") == 0) {
  723. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_lun);
  724. } else if (strcmp(attr->name, "num_planes") == 0) {
  725. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pln);
  726. } else if (strcmp(attr->name, "num_blocks") == 0) { /* u16 */
  727. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_blk);
  728. } else if (strcmp(attr->name, "num_pages") == 0) {
  729. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pg);
  730. } else if (strcmp(attr->name, "page_size") == 0) {
  731. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fpg_sz);
  732. } else if (strcmp(attr->name, "hw_sector_size") == 0) {
  733. return scnprintf(page, PAGE_SIZE, "%u\n", grp->csecs);
  734. } else if (strcmp(attr->name, "oob_sector_size") == 0) {/* u32 */
  735. return scnprintf(page, PAGE_SIZE, "%u\n", grp->sos);
  736. } else if (strcmp(attr->name, "read_typ") == 0) {
  737. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdt);
  738. } else if (strcmp(attr->name, "read_max") == 0) {
  739. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdm);
  740. } else if (strcmp(attr->name, "prog_typ") == 0) {
  741. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprt);
  742. } else if (strcmp(attr->name, "prog_max") == 0) {
  743. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprm);
  744. } else if (strcmp(attr->name, "erase_typ") == 0) {
  745. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbet);
  746. } else if (strcmp(attr->name, "erase_max") == 0) {
  747. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbem);
  748. } else if (strcmp(attr->name, "multiplane_modes") == 0) {
  749. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mpos);
  750. } else if (strcmp(attr->name, "media_capabilities") == 0) {
  751. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mccap);
  752. } else if (strcmp(attr->name, "max_phys_secs") == 0) {
  753. return scnprintf(page, PAGE_SIZE, "%u\n",
  754. ndev->ops->max_phys_sect);
  755. } else {
  756. return scnprintf(page,
  757. PAGE_SIZE,
  758. "Unhandled attr(%s) in `nvm_dev_attr_show`\n",
  759. attr->name);
  760. }
  761. }
  762. #define NVM_DEV_ATTR_RO(_name) \
  763. DEVICE_ATTR(_name, S_IRUGO, nvm_dev_attr_show, NULL)
  764. static NVM_DEV_ATTR_RO(version);
  765. static NVM_DEV_ATTR_RO(vendor_opcode);
  766. static NVM_DEV_ATTR_RO(capabilities);
  767. static NVM_DEV_ATTR_RO(device_mode);
  768. static NVM_DEV_ATTR_RO(ppa_format);
  769. static NVM_DEV_ATTR_RO(media_manager);
  770. static NVM_DEV_ATTR_RO(media_type);
  771. static NVM_DEV_ATTR_RO(flash_media_type);
  772. static NVM_DEV_ATTR_RO(num_channels);
  773. static NVM_DEV_ATTR_RO(num_luns);
  774. static NVM_DEV_ATTR_RO(num_planes);
  775. static NVM_DEV_ATTR_RO(num_blocks);
  776. static NVM_DEV_ATTR_RO(num_pages);
  777. static NVM_DEV_ATTR_RO(page_size);
  778. static NVM_DEV_ATTR_RO(hw_sector_size);
  779. static NVM_DEV_ATTR_RO(oob_sector_size);
  780. static NVM_DEV_ATTR_RO(read_typ);
  781. static NVM_DEV_ATTR_RO(read_max);
  782. static NVM_DEV_ATTR_RO(prog_typ);
  783. static NVM_DEV_ATTR_RO(prog_max);
  784. static NVM_DEV_ATTR_RO(erase_typ);
  785. static NVM_DEV_ATTR_RO(erase_max);
  786. static NVM_DEV_ATTR_RO(multiplane_modes);
  787. static NVM_DEV_ATTR_RO(media_capabilities);
  788. static NVM_DEV_ATTR_RO(max_phys_secs);
  789. static struct attribute *nvm_dev_attrs[] = {
  790. &dev_attr_version.attr,
  791. &dev_attr_vendor_opcode.attr,
  792. &dev_attr_capabilities.attr,
  793. &dev_attr_device_mode.attr,
  794. &dev_attr_media_manager.attr,
  795. &dev_attr_ppa_format.attr,
  796. &dev_attr_media_type.attr,
  797. &dev_attr_flash_media_type.attr,
  798. &dev_attr_num_channels.attr,
  799. &dev_attr_num_luns.attr,
  800. &dev_attr_num_planes.attr,
  801. &dev_attr_num_blocks.attr,
  802. &dev_attr_num_pages.attr,
  803. &dev_attr_page_size.attr,
  804. &dev_attr_hw_sector_size.attr,
  805. &dev_attr_oob_sector_size.attr,
  806. &dev_attr_read_typ.attr,
  807. &dev_attr_read_max.attr,
  808. &dev_attr_prog_typ.attr,
  809. &dev_attr_prog_max.attr,
  810. &dev_attr_erase_typ.attr,
  811. &dev_attr_erase_max.attr,
  812. &dev_attr_multiplane_modes.attr,
  813. &dev_attr_media_capabilities.attr,
  814. &dev_attr_max_phys_secs.attr,
  815. NULL,
  816. };
  817. static const struct attribute_group nvm_dev_attr_group = {
  818. .name = "lightnvm",
  819. .attrs = nvm_dev_attrs,
  820. };
  821. int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  822. {
  823. return sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  824. &nvm_dev_attr_group);
  825. }
  826. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns)
  827. {
  828. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  829. &nvm_dev_attr_group);
  830. }