lightnvm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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->head->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->head->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->head->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->head->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->head->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 struct request *nvme_nvm_alloc_request(struct request_queue *q,
  434. struct nvm_rq *rqd,
  435. struct nvme_nvm_command *cmd)
  436. {
  437. struct nvme_ns *ns = q->queuedata;
  438. struct request *rq;
  439. nvme_nvm_rqtocmd(rqd, ns, cmd);
  440. rq = nvme_alloc_request(q, (struct nvme_command *)cmd, 0, NVME_QID_ANY);
  441. if (IS_ERR(rq))
  442. return rq;
  443. rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
  444. if (rqd->bio) {
  445. blk_init_request_from_bio(rq, rqd->bio);
  446. } else {
  447. rq->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  448. rq->__data_len = 0;
  449. }
  450. return rq;
  451. }
  452. static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  453. {
  454. struct request_queue *q = dev->q;
  455. struct nvme_nvm_command *cmd;
  456. struct request *rq;
  457. cmd = kzalloc(sizeof(struct nvme_nvm_command), GFP_KERNEL);
  458. if (!cmd)
  459. return -ENOMEM;
  460. rq = nvme_nvm_alloc_request(q, rqd, cmd);
  461. if (IS_ERR(rq)) {
  462. kfree(cmd);
  463. return PTR_ERR(rq);
  464. }
  465. rq->end_io_data = rqd;
  466. blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io);
  467. return 0;
  468. }
  469. static int nvme_nvm_submit_io_sync(struct nvm_dev *dev, struct nvm_rq *rqd)
  470. {
  471. struct request_queue *q = dev->q;
  472. struct request *rq;
  473. struct nvme_nvm_command cmd;
  474. int ret = 0;
  475. memset(&cmd, 0, sizeof(struct nvme_nvm_command));
  476. rq = nvme_nvm_alloc_request(q, rqd, &cmd);
  477. if (IS_ERR(rq))
  478. return PTR_ERR(rq);
  479. /* I/Os can fail and the error is signaled through rqd. Callers must
  480. * handle the error accordingly.
  481. */
  482. blk_execute_rq(q, NULL, rq, 0);
  483. if (nvme_req(rq)->flags & NVME_REQ_CANCELLED)
  484. ret = -EINTR;
  485. rqd->ppa_status = le64_to_cpu(nvme_req(rq)->result.u64);
  486. rqd->error = nvme_req(rq)->status;
  487. blk_mq_free_request(rq);
  488. return ret;
  489. }
  490. static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name)
  491. {
  492. struct nvme_ns *ns = nvmdev->q->queuedata;
  493. return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0);
  494. }
  495. static void nvme_nvm_destroy_dma_pool(void *pool)
  496. {
  497. struct dma_pool *dma_pool = pool;
  498. dma_pool_destroy(dma_pool);
  499. }
  500. static void *nvme_nvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  501. gfp_t mem_flags, dma_addr_t *dma_handler)
  502. {
  503. return dma_pool_alloc(pool, mem_flags, dma_handler);
  504. }
  505. static void nvme_nvm_dev_dma_free(void *pool, void *addr,
  506. dma_addr_t dma_handler)
  507. {
  508. dma_pool_free(pool, addr, dma_handler);
  509. }
  510. static struct nvm_dev_ops nvme_nvm_dev_ops = {
  511. .identity = nvme_nvm_identity,
  512. .get_l2p_tbl = nvme_nvm_get_l2p_tbl,
  513. .get_bb_tbl = nvme_nvm_get_bb_tbl,
  514. .set_bb_tbl = nvme_nvm_set_bb_tbl,
  515. .submit_io = nvme_nvm_submit_io,
  516. .submit_io_sync = nvme_nvm_submit_io_sync,
  517. .create_dma_pool = nvme_nvm_create_dma_pool,
  518. .destroy_dma_pool = nvme_nvm_destroy_dma_pool,
  519. .dev_dma_alloc = nvme_nvm_dev_dma_alloc,
  520. .dev_dma_free = nvme_nvm_dev_dma_free,
  521. .max_phys_sect = 64,
  522. };
  523. static int nvme_nvm_submit_user_cmd(struct request_queue *q,
  524. struct nvme_ns *ns,
  525. struct nvme_nvm_command *vcmd,
  526. void __user *ubuf, unsigned int bufflen,
  527. void __user *meta_buf, unsigned int meta_len,
  528. void __user *ppa_buf, unsigned int ppa_len,
  529. u32 *result, u64 *status, unsigned int timeout)
  530. {
  531. bool write = nvme_is_write((struct nvme_command *)vcmd);
  532. struct nvm_dev *dev = ns->ndev;
  533. struct gendisk *disk = ns->disk;
  534. struct request *rq;
  535. struct bio *bio = NULL;
  536. __le64 *ppa_list = NULL;
  537. dma_addr_t ppa_dma;
  538. __le64 *metadata = NULL;
  539. dma_addr_t metadata_dma;
  540. DECLARE_COMPLETION_ONSTACK(wait);
  541. int ret = 0;
  542. rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0,
  543. NVME_QID_ANY);
  544. if (IS_ERR(rq)) {
  545. ret = -ENOMEM;
  546. goto err_cmd;
  547. }
  548. rq->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  549. if (ppa_buf && ppa_len) {
  550. ppa_list = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, &ppa_dma);
  551. if (!ppa_list) {
  552. ret = -ENOMEM;
  553. goto err_rq;
  554. }
  555. if (copy_from_user(ppa_list, (void __user *)ppa_buf,
  556. sizeof(u64) * (ppa_len + 1))) {
  557. ret = -EFAULT;
  558. goto err_ppa;
  559. }
  560. vcmd->ph_rw.spba = cpu_to_le64(ppa_dma);
  561. } else {
  562. vcmd->ph_rw.spba = cpu_to_le64((uintptr_t)ppa_buf);
  563. }
  564. if (ubuf && bufflen) {
  565. ret = blk_rq_map_user(q, rq, NULL, ubuf, bufflen, GFP_KERNEL);
  566. if (ret)
  567. goto err_ppa;
  568. bio = rq->bio;
  569. if (meta_buf && meta_len) {
  570. metadata = dma_pool_alloc(dev->dma_pool, GFP_KERNEL,
  571. &metadata_dma);
  572. if (!metadata) {
  573. ret = -ENOMEM;
  574. goto err_map;
  575. }
  576. if (write) {
  577. if (copy_from_user(metadata,
  578. (void __user *)meta_buf,
  579. meta_len)) {
  580. ret = -EFAULT;
  581. goto err_meta;
  582. }
  583. }
  584. vcmd->ph_rw.metadata = cpu_to_le64(metadata_dma);
  585. }
  586. bio->bi_disk = disk;
  587. }
  588. blk_execute_rq(q, NULL, rq, 0);
  589. if (nvme_req(rq)->flags & NVME_REQ_CANCELLED)
  590. ret = -EINTR;
  591. else if (nvme_req(rq)->status & 0x7ff)
  592. ret = -EIO;
  593. if (result)
  594. *result = nvme_req(rq)->status & 0x7ff;
  595. if (status)
  596. *status = le64_to_cpu(nvme_req(rq)->result.u64);
  597. if (metadata && !ret && !write) {
  598. if (copy_to_user(meta_buf, (void *)metadata, meta_len))
  599. ret = -EFAULT;
  600. }
  601. err_meta:
  602. if (meta_buf && meta_len)
  603. dma_pool_free(dev->dma_pool, metadata, metadata_dma);
  604. err_map:
  605. if (bio)
  606. blk_rq_unmap_user(bio);
  607. err_ppa:
  608. if (ppa_buf && ppa_len)
  609. dma_pool_free(dev->dma_pool, ppa_list, ppa_dma);
  610. err_rq:
  611. blk_mq_free_request(rq);
  612. err_cmd:
  613. return ret;
  614. }
  615. static int nvme_nvm_submit_vio(struct nvme_ns *ns,
  616. struct nvm_user_vio __user *uvio)
  617. {
  618. struct nvm_user_vio vio;
  619. struct nvme_nvm_command c;
  620. unsigned int length;
  621. int ret;
  622. if (copy_from_user(&vio, uvio, sizeof(vio)))
  623. return -EFAULT;
  624. if (vio.flags)
  625. return -EINVAL;
  626. memset(&c, 0, sizeof(c));
  627. c.ph_rw.opcode = vio.opcode;
  628. c.ph_rw.nsid = cpu_to_le32(ns->head->ns_id);
  629. c.ph_rw.control = cpu_to_le16(vio.control);
  630. c.ph_rw.length = cpu_to_le16(vio.nppas);
  631. length = (vio.nppas + 1) << ns->lba_shift;
  632. ret = nvme_nvm_submit_user_cmd(ns->queue, ns, &c,
  633. (void __user *)(uintptr_t)vio.addr, length,
  634. (void __user *)(uintptr_t)vio.metadata,
  635. vio.metadata_len,
  636. (void __user *)(uintptr_t)vio.ppa_list, vio.nppas,
  637. &vio.result, &vio.status, 0);
  638. if (ret && copy_to_user(uvio, &vio, sizeof(vio)))
  639. return -EFAULT;
  640. return ret;
  641. }
  642. static int nvme_nvm_user_vcmd(struct nvme_ns *ns, int admin,
  643. struct nvm_passthru_vio __user *uvcmd)
  644. {
  645. struct nvm_passthru_vio vcmd;
  646. struct nvme_nvm_command c;
  647. struct request_queue *q;
  648. unsigned int timeout = 0;
  649. int ret;
  650. if (copy_from_user(&vcmd, uvcmd, sizeof(vcmd)))
  651. return -EFAULT;
  652. if ((vcmd.opcode != 0xF2) && (!capable(CAP_SYS_ADMIN)))
  653. return -EACCES;
  654. if (vcmd.flags)
  655. return -EINVAL;
  656. memset(&c, 0, sizeof(c));
  657. c.common.opcode = vcmd.opcode;
  658. c.common.nsid = cpu_to_le32(ns->head->ns_id);
  659. c.common.cdw2[0] = cpu_to_le32(vcmd.cdw2);
  660. c.common.cdw2[1] = cpu_to_le32(vcmd.cdw3);
  661. /* cdw11-12 */
  662. c.ph_rw.length = cpu_to_le16(vcmd.nppas);
  663. c.ph_rw.control = cpu_to_le16(vcmd.control);
  664. c.common.cdw10[3] = cpu_to_le32(vcmd.cdw13);
  665. c.common.cdw10[4] = cpu_to_le32(vcmd.cdw14);
  666. c.common.cdw10[5] = cpu_to_le32(vcmd.cdw15);
  667. if (vcmd.timeout_ms)
  668. timeout = msecs_to_jiffies(vcmd.timeout_ms);
  669. q = admin ? ns->ctrl->admin_q : ns->queue;
  670. ret = nvme_nvm_submit_user_cmd(q, ns,
  671. (struct nvme_nvm_command *)&c,
  672. (void __user *)(uintptr_t)vcmd.addr, vcmd.data_len,
  673. (void __user *)(uintptr_t)vcmd.metadata,
  674. vcmd.metadata_len,
  675. (void __user *)(uintptr_t)vcmd.ppa_list, vcmd.nppas,
  676. &vcmd.result, &vcmd.status, timeout);
  677. if (ret && copy_to_user(uvcmd, &vcmd, sizeof(vcmd)))
  678. return -EFAULT;
  679. return ret;
  680. }
  681. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg)
  682. {
  683. switch (cmd) {
  684. case NVME_NVM_IOCTL_ADMIN_VIO:
  685. return nvme_nvm_user_vcmd(ns, 1, (void __user *)arg);
  686. case NVME_NVM_IOCTL_IO_VIO:
  687. return nvme_nvm_user_vcmd(ns, 0, (void __user *)arg);
  688. case NVME_NVM_IOCTL_SUBMIT_VIO:
  689. return nvme_nvm_submit_vio(ns, (void __user *)arg);
  690. default:
  691. return -ENOTTY;
  692. }
  693. }
  694. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node)
  695. {
  696. struct request_queue *q = ns->queue;
  697. struct nvm_dev *dev;
  698. _nvme_nvm_check_size();
  699. dev = nvm_alloc_dev(node);
  700. if (!dev)
  701. return -ENOMEM;
  702. dev->q = q;
  703. memcpy(dev->name, disk_name, DISK_NAME_LEN);
  704. dev->ops = &nvme_nvm_dev_ops;
  705. dev->private_data = ns;
  706. ns->ndev = dev;
  707. return nvm_register(dev);
  708. }
  709. void nvme_nvm_unregister(struct nvme_ns *ns)
  710. {
  711. nvm_unregister(ns->ndev);
  712. }
  713. static ssize_t nvm_dev_attr_show(struct device *dev,
  714. struct device_attribute *dattr, char *page)
  715. {
  716. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  717. struct nvm_dev *ndev = ns->ndev;
  718. struct nvm_id *id;
  719. struct nvm_id_group *grp;
  720. struct attribute *attr;
  721. if (!ndev)
  722. return 0;
  723. id = &ndev->identity;
  724. grp = &id->grp;
  725. attr = &dattr->attr;
  726. if (strcmp(attr->name, "version") == 0) {
  727. return scnprintf(page, PAGE_SIZE, "%u\n", id->ver_id);
  728. } else if (strcmp(attr->name, "vendor_opcode") == 0) {
  729. return scnprintf(page, PAGE_SIZE, "%u\n", id->vmnt);
  730. } else if (strcmp(attr->name, "capabilities") == 0) {
  731. return scnprintf(page, PAGE_SIZE, "%u\n", id->cap);
  732. } else if (strcmp(attr->name, "device_mode") == 0) {
  733. return scnprintf(page, PAGE_SIZE, "%u\n", id->dom);
  734. /* kept for compatibility */
  735. } else if (strcmp(attr->name, "media_manager") == 0) {
  736. return scnprintf(page, PAGE_SIZE, "%s\n", "gennvm");
  737. } else if (strcmp(attr->name, "ppa_format") == 0) {
  738. return scnprintf(page, PAGE_SIZE,
  739. "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  740. id->ppaf.ch_offset, id->ppaf.ch_len,
  741. id->ppaf.lun_offset, id->ppaf.lun_len,
  742. id->ppaf.pln_offset, id->ppaf.pln_len,
  743. id->ppaf.blk_offset, id->ppaf.blk_len,
  744. id->ppaf.pg_offset, id->ppaf.pg_len,
  745. id->ppaf.sect_offset, id->ppaf.sect_len);
  746. } else if (strcmp(attr->name, "media_type") == 0) { /* u8 */
  747. return scnprintf(page, PAGE_SIZE, "%u\n", grp->mtype);
  748. } else if (strcmp(attr->name, "flash_media_type") == 0) {
  749. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fmtype);
  750. } else if (strcmp(attr->name, "num_channels") == 0) {
  751. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_ch);
  752. } else if (strcmp(attr->name, "num_luns") == 0) {
  753. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_lun);
  754. } else if (strcmp(attr->name, "num_planes") == 0) {
  755. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pln);
  756. } else if (strcmp(attr->name, "num_blocks") == 0) { /* u16 */
  757. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_blk);
  758. } else if (strcmp(attr->name, "num_pages") == 0) {
  759. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pg);
  760. } else if (strcmp(attr->name, "page_size") == 0) {
  761. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fpg_sz);
  762. } else if (strcmp(attr->name, "hw_sector_size") == 0) {
  763. return scnprintf(page, PAGE_SIZE, "%u\n", grp->csecs);
  764. } else if (strcmp(attr->name, "oob_sector_size") == 0) {/* u32 */
  765. return scnprintf(page, PAGE_SIZE, "%u\n", grp->sos);
  766. } else if (strcmp(attr->name, "read_typ") == 0) {
  767. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdt);
  768. } else if (strcmp(attr->name, "read_max") == 0) {
  769. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdm);
  770. } else if (strcmp(attr->name, "prog_typ") == 0) {
  771. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprt);
  772. } else if (strcmp(attr->name, "prog_max") == 0) {
  773. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprm);
  774. } else if (strcmp(attr->name, "erase_typ") == 0) {
  775. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbet);
  776. } else if (strcmp(attr->name, "erase_max") == 0) {
  777. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbem);
  778. } else if (strcmp(attr->name, "multiplane_modes") == 0) {
  779. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mpos);
  780. } else if (strcmp(attr->name, "media_capabilities") == 0) {
  781. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mccap);
  782. } else if (strcmp(attr->name, "max_phys_secs") == 0) {
  783. return scnprintf(page, PAGE_SIZE, "%u\n",
  784. ndev->ops->max_phys_sect);
  785. } else {
  786. return scnprintf(page,
  787. PAGE_SIZE,
  788. "Unhandled attr(%s) in `nvm_dev_attr_show`\n",
  789. attr->name);
  790. }
  791. }
  792. #define NVM_DEV_ATTR_RO(_name) \
  793. DEVICE_ATTR(_name, S_IRUGO, nvm_dev_attr_show, NULL)
  794. static NVM_DEV_ATTR_RO(version);
  795. static NVM_DEV_ATTR_RO(vendor_opcode);
  796. static NVM_DEV_ATTR_RO(capabilities);
  797. static NVM_DEV_ATTR_RO(device_mode);
  798. static NVM_DEV_ATTR_RO(ppa_format);
  799. static NVM_DEV_ATTR_RO(media_manager);
  800. static NVM_DEV_ATTR_RO(media_type);
  801. static NVM_DEV_ATTR_RO(flash_media_type);
  802. static NVM_DEV_ATTR_RO(num_channels);
  803. static NVM_DEV_ATTR_RO(num_luns);
  804. static NVM_DEV_ATTR_RO(num_planes);
  805. static NVM_DEV_ATTR_RO(num_blocks);
  806. static NVM_DEV_ATTR_RO(num_pages);
  807. static NVM_DEV_ATTR_RO(page_size);
  808. static NVM_DEV_ATTR_RO(hw_sector_size);
  809. static NVM_DEV_ATTR_RO(oob_sector_size);
  810. static NVM_DEV_ATTR_RO(read_typ);
  811. static NVM_DEV_ATTR_RO(read_max);
  812. static NVM_DEV_ATTR_RO(prog_typ);
  813. static NVM_DEV_ATTR_RO(prog_max);
  814. static NVM_DEV_ATTR_RO(erase_typ);
  815. static NVM_DEV_ATTR_RO(erase_max);
  816. static NVM_DEV_ATTR_RO(multiplane_modes);
  817. static NVM_DEV_ATTR_RO(media_capabilities);
  818. static NVM_DEV_ATTR_RO(max_phys_secs);
  819. static struct attribute *nvm_dev_attrs[] = {
  820. &dev_attr_version.attr,
  821. &dev_attr_vendor_opcode.attr,
  822. &dev_attr_capabilities.attr,
  823. &dev_attr_device_mode.attr,
  824. &dev_attr_media_manager.attr,
  825. &dev_attr_ppa_format.attr,
  826. &dev_attr_media_type.attr,
  827. &dev_attr_flash_media_type.attr,
  828. &dev_attr_num_channels.attr,
  829. &dev_attr_num_luns.attr,
  830. &dev_attr_num_planes.attr,
  831. &dev_attr_num_blocks.attr,
  832. &dev_attr_num_pages.attr,
  833. &dev_attr_page_size.attr,
  834. &dev_attr_hw_sector_size.attr,
  835. &dev_attr_oob_sector_size.attr,
  836. &dev_attr_read_typ.attr,
  837. &dev_attr_read_max.attr,
  838. &dev_attr_prog_typ.attr,
  839. &dev_attr_prog_max.attr,
  840. &dev_attr_erase_typ.attr,
  841. &dev_attr_erase_max.attr,
  842. &dev_attr_multiplane_modes.attr,
  843. &dev_attr_media_capabilities.attr,
  844. &dev_attr_max_phys_secs.attr,
  845. NULL,
  846. };
  847. static const struct attribute_group nvm_dev_attr_group = {
  848. .name = "lightnvm",
  849. .attrs = nvm_dev_attrs,
  850. };
  851. int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  852. {
  853. return sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  854. &nvm_dev_attr_group);
  855. }
  856. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns)
  857. {
  858. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  859. &nvm_dev_attr_group);
  860. }