lightnvm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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) != 128);
  227. BUILD_BUG_ON(sizeof(struct nvme_nvm_id) != 4096);
  228. BUILD_BUG_ON(sizeof(struct nvme_nvm_bb_tbl) != 512);
  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 nvme_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. return -EINVAL;
  330. }
  331. /* Transform physical address to target address space */
  332. nvm_part_to_tgt(nvmdev, entries, cmd_nlb);
  333. if (update_l2p(cmd_slba, cmd_nlb, entries, priv)) {
  334. ret = -EINTR;
  335. goto out;
  336. }
  337. cmd_slba += cmd_nlb;
  338. nlb -= cmd_nlb;
  339. }
  340. out:
  341. kfree(entries);
  342. return ret;
  343. }
  344. static int nvme_nvm_get_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr ppa,
  345. u8 *blks)
  346. {
  347. struct request_queue *q = nvmdev->q;
  348. struct nvm_geo *geo = &nvmdev->geo;
  349. struct nvme_ns *ns = q->queuedata;
  350. struct nvme_ctrl *ctrl = ns->ctrl;
  351. struct nvme_nvm_command c = {};
  352. struct nvme_nvm_bb_tbl *bb_tbl;
  353. int nr_blks = geo->blks_per_lun * geo->plane_mode;
  354. int tblsz = sizeof(struct nvme_nvm_bb_tbl) + nr_blks;
  355. int ret = 0;
  356. c.get_bb.opcode = nvme_nvm_admin_get_bb_tbl;
  357. c.get_bb.nsid = cpu_to_le32(ns->ns_id);
  358. c.get_bb.spba = cpu_to_le64(ppa.ppa);
  359. bb_tbl = kzalloc(tblsz, GFP_KERNEL);
  360. if (!bb_tbl)
  361. return -ENOMEM;
  362. ret = nvme_submit_sync_cmd(ctrl->admin_q, (struct nvme_command *)&c,
  363. bb_tbl, tblsz);
  364. if (ret) {
  365. dev_err(ctrl->device, "get bad block table failed (%d)\n", ret);
  366. ret = -EIO;
  367. goto out;
  368. }
  369. if (bb_tbl->tblid[0] != 'B' || bb_tbl->tblid[1] != 'B' ||
  370. bb_tbl->tblid[2] != 'L' || bb_tbl->tblid[3] != 'T') {
  371. dev_err(ctrl->device, "bbt format mismatch\n");
  372. ret = -EINVAL;
  373. goto out;
  374. }
  375. if (le16_to_cpu(bb_tbl->verid) != 1) {
  376. ret = -EINVAL;
  377. dev_err(ctrl->device, "bbt version not supported\n");
  378. goto out;
  379. }
  380. if (le32_to_cpu(bb_tbl->tblks) != nr_blks) {
  381. ret = -EINVAL;
  382. dev_err(ctrl->device,
  383. "bbt unsuspected blocks returned (%u!=%u)",
  384. le32_to_cpu(bb_tbl->tblks), nr_blks);
  385. goto out;
  386. }
  387. memcpy(blks, bb_tbl->blk, geo->blks_per_lun * geo->plane_mode);
  388. out:
  389. kfree(bb_tbl);
  390. return ret;
  391. }
  392. static int nvme_nvm_set_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr *ppas,
  393. int nr_ppas, int type)
  394. {
  395. struct nvme_ns *ns = nvmdev->q->queuedata;
  396. struct nvme_nvm_command c = {};
  397. int ret = 0;
  398. c.set_bb.opcode = nvme_nvm_admin_set_bb_tbl;
  399. c.set_bb.nsid = cpu_to_le32(ns->ns_id);
  400. c.set_bb.spba = cpu_to_le64(ppas->ppa);
  401. c.set_bb.nlb = cpu_to_le16(nr_ppas - 1);
  402. c.set_bb.value = type;
  403. ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c,
  404. NULL, 0);
  405. if (ret)
  406. dev_err(ns->ctrl->device, "set bad block table failed (%d)\n",
  407. ret);
  408. return ret;
  409. }
  410. static inline void nvme_nvm_rqtocmd(struct request *rq, struct nvm_rq *rqd,
  411. struct nvme_ns *ns, struct nvme_nvm_command *c)
  412. {
  413. c->ph_rw.opcode = rqd->opcode;
  414. c->ph_rw.nsid = cpu_to_le32(ns->ns_id);
  415. c->ph_rw.spba = cpu_to_le64(rqd->ppa_addr.ppa);
  416. c->ph_rw.metadata = cpu_to_le64(rqd->dma_meta_list);
  417. c->ph_rw.control = cpu_to_le16(rqd->flags);
  418. c->ph_rw.length = cpu_to_le16(rqd->nr_ppas - 1);
  419. if (rqd->opcode == NVM_OP_HBWRITE || rqd->opcode == NVM_OP_HBREAD)
  420. c->hb_rw.slba = cpu_to_le64(nvme_block_nr(ns,
  421. rqd->bio->bi_iter.bi_sector));
  422. }
  423. static void nvme_nvm_end_io(struct request *rq, int error)
  424. {
  425. struct nvm_rq *rqd = rq->end_io_data;
  426. rqd->ppa_status = nvme_req(rq)->result.u64;
  427. rqd->error = error;
  428. nvm_end_io(rqd);
  429. kfree(nvme_req(rq)->cmd);
  430. blk_mq_free_request(rq);
  431. }
  432. static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  433. {
  434. struct request_queue *q = dev->q;
  435. struct nvme_ns *ns = q->queuedata;
  436. struct request *rq;
  437. struct bio *bio = rqd->bio;
  438. struct nvme_nvm_command *cmd;
  439. cmd = kzalloc(sizeof(struct nvme_nvm_command), GFP_KERNEL);
  440. if (!cmd)
  441. return -ENOMEM;
  442. rq = nvme_alloc_request(q, (struct nvme_command *)cmd, 0, NVME_QID_ANY);
  443. if (IS_ERR(rq)) {
  444. kfree(cmd);
  445. return -ENOMEM;
  446. }
  447. rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
  448. rq->ioprio = bio_prio(bio);
  449. if (bio_has_data(bio))
  450. rq->nr_phys_segments = bio_phys_segments(q, bio);
  451. rq->__data_len = bio->bi_iter.bi_size;
  452. rq->bio = rq->biotail = bio;
  453. nvme_nvm_rqtocmd(rq, rqd, ns, cmd);
  454. rq->end_io_data = rqd;
  455. blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io);
  456. return 0;
  457. }
  458. static int nvme_nvm_erase_block(struct nvm_dev *dev, struct nvm_rq *rqd)
  459. {
  460. struct request_queue *q = dev->q;
  461. struct nvme_ns *ns = q->queuedata;
  462. struct nvme_nvm_command c = {};
  463. c.erase.opcode = NVM_OP_ERASE;
  464. c.erase.nsid = cpu_to_le32(ns->ns_id);
  465. c.erase.spba = cpu_to_le64(rqd->ppa_addr.ppa);
  466. c.erase.length = cpu_to_le16(rqd->nr_ppas - 1);
  467. c.erase.control = cpu_to_le16(rqd->flags);
  468. return nvme_submit_sync_cmd(q, (struct nvme_command *)&c, NULL, 0);
  469. }
  470. static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name)
  471. {
  472. struct nvme_ns *ns = nvmdev->q->queuedata;
  473. return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0);
  474. }
  475. static void nvme_nvm_destroy_dma_pool(void *pool)
  476. {
  477. struct dma_pool *dma_pool = pool;
  478. dma_pool_destroy(dma_pool);
  479. }
  480. static void *nvme_nvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  481. gfp_t mem_flags, dma_addr_t *dma_handler)
  482. {
  483. return dma_pool_alloc(pool, mem_flags, dma_handler);
  484. }
  485. static void nvme_nvm_dev_dma_free(void *pool, void *addr,
  486. dma_addr_t dma_handler)
  487. {
  488. dma_pool_free(pool, addr, dma_handler);
  489. }
  490. static struct nvm_dev_ops nvme_nvm_dev_ops = {
  491. .identity = nvme_nvm_identity,
  492. .get_l2p_tbl = nvme_nvm_get_l2p_tbl,
  493. .get_bb_tbl = nvme_nvm_get_bb_tbl,
  494. .set_bb_tbl = nvme_nvm_set_bb_tbl,
  495. .submit_io = nvme_nvm_submit_io,
  496. .erase_block = nvme_nvm_erase_block,
  497. .create_dma_pool = nvme_nvm_create_dma_pool,
  498. .destroy_dma_pool = nvme_nvm_destroy_dma_pool,
  499. .dev_dma_alloc = nvme_nvm_dev_dma_alloc,
  500. .dev_dma_free = nvme_nvm_dev_dma_free,
  501. .max_phys_sect = 64,
  502. };
  503. static void nvme_nvm_end_user_vio(struct request *rq, int error)
  504. {
  505. struct completion *waiting = rq->end_io_data;
  506. complete(waiting);
  507. }
  508. static int nvme_nvm_submit_user_cmd(struct request_queue *q,
  509. struct nvme_ns *ns,
  510. struct nvme_nvm_command *vcmd,
  511. void __user *ubuf, unsigned int bufflen,
  512. void __user *meta_buf, unsigned int meta_len,
  513. void __user *ppa_buf, unsigned int ppa_len,
  514. u32 *result, u64 *status, unsigned int timeout)
  515. {
  516. bool write = nvme_is_write((struct nvme_command *)vcmd);
  517. struct nvm_dev *dev = ns->ndev;
  518. struct gendisk *disk = ns->disk;
  519. struct request *rq;
  520. struct bio *bio = NULL;
  521. __le64 *ppa_list = NULL;
  522. dma_addr_t ppa_dma;
  523. __le64 *metadata = NULL;
  524. dma_addr_t metadata_dma;
  525. DECLARE_COMPLETION_ONSTACK(wait);
  526. int ret;
  527. rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0,
  528. NVME_QID_ANY);
  529. if (IS_ERR(rq)) {
  530. ret = -ENOMEM;
  531. goto err_cmd;
  532. }
  533. rq->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  534. rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
  535. rq->end_io_data = &wait;
  536. if (ppa_buf && ppa_len) {
  537. ppa_list = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, &ppa_dma);
  538. if (!ppa_list) {
  539. ret = -ENOMEM;
  540. goto err_rq;
  541. }
  542. if (copy_from_user(ppa_list, (void __user *)ppa_buf,
  543. sizeof(u64) * (ppa_len + 1))) {
  544. ret = -EFAULT;
  545. goto err_ppa;
  546. }
  547. vcmd->ph_rw.spba = cpu_to_le64(ppa_dma);
  548. } else {
  549. vcmd->ph_rw.spba = cpu_to_le64((uintptr_t)ppa_buf);
  550. }
  551. if (ubuf && bufflen) {
  552. ret = blk_rq_map_user(q, rq, NULL, ubuf, bufflen, GFP_KERNEL);
  553. if (ret)
  554. goto err_ppa;
  555. bio = rq->bio;
  556. if (meta_buf && meta_len) {
  557. metadata = dma_pool_alloc(dev->dma_pool, GFP_KERNEL,
  558. &metadata_dma);
  559. if (!metadata) {
  560. ret = -ENOMEM;
  561. goto err_map;
  562. }
  563. if (write) {
  564. if (copy_from_user(metadata,
  565. (void __user *)meta_buf,
  566. meta_len)) {
  567. ret = -EFAULT;
  568. goto err_meta;
  569. }
  570. }
  571. vcmd->ph_rw.metadata = cpu_to_le64(metadata_dma);
  572. }
  573. if (!disk)
  574. goto submit;
  575. bio->bi_bdev = bdget_disk(disk, 0);
  576. if (!bio->bi_bdev) {
  577. ret = -ENODEV;
  578. goto err_meta;
  579. }
  580. }
  581. submit:
  582. blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_user_vio);
  583. wait_for_completion_io(&wait);
  584. ret = nvme_error_status(rq->errors);
  585. if (result)
  586. *result = rq->errors & 0x7ff;
  587. if (status)
  588. *status = le64_to_cpu(nvme_req(rq)->result.u64);
  589. if (metadata && !ret && !write) {
  590. if (copy_to_user(meta_buf, (void *)metadata, meta_len))
  591. ret = -EFAULT;
  592. }
  593. err_meta:
  594. if (meta_buf && meta_len)
  595. dma_pool_free(dev->dma_pool, metadata, metadata_dma);
  596. err_map:
  597. if (bio) {
  598. if (disk && bio->bi_bdev)
  599. bdput(bio->bi_bdev);
  600. blk_rq_unmap_user(bio);
  601. }
  602. err_ppa:
  603. if (ppa_buf && ppa_len)
  604. dma_pool_free(dev->dma_pool, ppa_list, ppa_dma);
  605. err_rq:
  606. blk_mq_free_request(rq);
  607. err_cmd:
  608. return ret;
  609. }
  610. static int nvme_nvm_submit_vio(struct nvme_ns *ns,
  611. struct nvm_user_vio __user *uvio)
  612. {
  613. struct nvm_user_vio vio;
  614. struct nvme_nvm_command c;
  615. unsigned int length;
  616. int ret;
  617. if (copy_from_user(&vio, uvio, sizeof(vio)))
  618. return -EFAULT;
  619. if (vio.flags)
  620. return -EINVAL;
  621. memset(&c, 0, sizeof(c));
  622. c.ph_rw.opcode = vio.opcode;
  623. c.ph_rw.nsid = cpu_to_le32(ns->ns_id);
  624. c.ph_rw.control = cpu_to_le16(vio.control);
  625. c.ph_rw.length = cpu_to_le16(vio.nppas);
  626. length = (vio.nppas + 1) << ns->lba_shift;
  627. ret = nvme_nvm_submit_user_cmd(ns->queue, ns, &c,
  628. (void __user *)(uintptr_t)vio.addr, length,
  629. (void __user *)(uintptr_t)vio.metadata,
  630. vio.metadata_len,
  631. (void __user *)(uintptr_t)vio.ppa_list, vio.nppas,
  632. &vio.result, &vio.status, 0);
  633. if (ret && copy_to_user(uvio, &vio, sizeof(vio)))
  634. return -EFAULT;
  635. return ret;
  636. }
  637. static int nvme_nvm_user_vcmd(struct nvme_ns *ns, int admin,
  638. struct nvm_passthru_vio __user *uvcmd)
  639. {
  640. struct nvm_passthru_vio vcmd;
  641. struct nvme_nvm_command c;
  642. struct request_queue *q;
  643. unsigned int timeout = 0;
  644. int ret;
  645. if (copy_from_user(&vcmd, uvcmd, sizeof(vcmd)))
  646. return -EFAULT;
  647. if ((vcmd.opcode != 0xF2) && (!capable(CAP_SYS_ADMIN)))
  648. return -EACCES;
  649. if (vcmd.flags)
  650. return -EINVAL;
  651. memset(&c, 0, sizeof(c));
  652. c.common.opcode = vcmd.opcode;
  653. c.common.nsid = cpu_to_le32(ns->ns_id);
  654. c.common.cdw2[0] = cpu_to_le32(vcmd.cdw2);
  655. c.common.cdw2[1] = cpu_to_le32(vcmd.cdw3);
  656. /* cdw11-12 */
  657. c.ph_rw.length = cpu_to_le16(vcmd.nppas);
  658. c.ph_rw.control = cpu_to_le32(vcmd.control);
  659. c.common.cdw10[3] = cpu_to_le32(vcmd.cdw13);
  660. c.common.cdw10[4] = cpu_to_le32(vcmd.cdw14);
  661. c.common.cdw10[5] = cpu_to_le32(vcmd.cdw15);
  662. if (vcmd.timeout_ms)
  663. timeout = msecs_to_jiffies(vcmd.timeout_ms);
  664. q = admin ? ns->ctrl->admin_q : ns->queue;
  665. ret = nvme_nvm_submit_user_cmd(q, ns,
  666. (struct nvme_nvm_command *)&c,
  667. (void __user *)(uintptr_t)vcmd.addr, vcmd.data_len,
  668. (void __user *)(uintptr_t)vcmd.metadata,
  669. vcmd.metadata_len,
  670. (void __user *)(uintptr_t)vcmd.ppa_list, vcmd.nppas,
  671. &vcmd.result, &vcmd.status, timeout);
  672. if (ret && copy_to_user(uvcmd, &vcmd, sizeof(vcmd)))
  673. return -EFAULT;
  674. return ret;
  675. }
  676. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg)
  677. {
  678. switch (cmd) {
  679. case NVME_NVM_IOCTL_ADMIN_VIO:
  680. return nvme_nvm_user_vcmd(ns, 1, (void __user *)arg);
  681. case NVME_NVM_IOCTL_IO_VIO:
  682. return nvme_nvm_user_vcmd(ns, 0, (void __user *)arg);
  683. case NVME_NVM_IOCTL_SUBMIT_VIO:
  684. return nvme_nvm_submit_vio(ns, (void __user *)arg);
  685. default:
  686. return -ENOTTY;
  687. }
  688. }
  689. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node)
  690. {
  691. struct request_queue *q = ns->queue;
  692. struct nvm_dev *dev;
  693. dev = nvm_alloc_dev(node);
  694. if (!dev)
  695. return -ENOMEM;
  696. dev->q = q;
  697. memcpy(dev->name, disk_name, DISK_NAME_LEN);
  698. dev->ops = &nvme_nvm_dev_ops;
  699. dev->private_data = ns;
  700. ns->ndev = dev;
  701. return nvm_register(dev);
  702. }
  703. void nvme_nvm_unregister(struct nvme_ns *ns)
  704. {
  705. nvm_unregister(ns->ndev);
  706. }
  707. static ssize_t nvm_dev_attr_show(struct device *dev,
  708. struct device_attribute *dattr, char *page)
  709. {
  710. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  711. struct nvm_dev *ndev = ns->ndev;
  712. struct nvm_id *id;
  713. struct nvm_id_group *grp;
  714. struct attribute *attr;
  715. if (!ndev)
  716. return 0;
  717. id = &ndev->identity;
  718. grp = &id->grp;
  719. attr = &dattr->attr;
  720. if (strcmp(attr->name, "version") == 0) {
  721. return scnprintf(page, PAGE_SIZE, "%u\n", id->ver_id);
  722. } else if (strcmp(attr->name, "vendor_opcode") == 0) {
  723. return scnprintf(page, PAGE_SIZE, "%u\n", id->vmnt);
  724. } else if (strcmp(attr->name, "capabilities") == 0) {
  725. return scnprintf(page, PAGE_SIZE, "%u\n", id->cap);
  726. } else if (strcmp(attr->name, "device_mode") == 0) {
  727. return scnprintf(page, PAGE_SIZE, "%u\n", id->dom);
  728. /* kept for compatibility */
  729. } else if (strcmp(attr->name, "media_manager") == 0) {
  730. return scnprintf(page, PAGE_SIZE, "%s\n", "gennvm");
  731. } else if (strcmp(attr->name, "ppa_format") == 0) {
  732. return scnprintf(page, PAGE_SIZE,
  733. "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  734. id->ppaf.ch_offset, id->ppaf.ch_len,
  735. id->ppaf.lun_offset, id->ppaf.lun_len,
  736. id->ppaf.pln_offset, id->ppaf.pln_len,
  737. id->ppaf.blk_offset, id->ppaf.blk_len,
  738. id->ppaf.pg_offset, id->ppaf.pg_len,
  739. id->ppaf.sect_offset, id->ppaf.sect_len);
  740. } else if (strcmp(attr->name, "media_type") == 0) { /* u8 */
  741. return scnprintf(page, PAGE_SIZE, "%u\n", grp->mtype);
  742. } else if (strcmp(attr->name, "flash_media_type") == 0) {
  743. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fmtype);
  744. } else if (strcmp(attr->name, "num_channels") == 0) {
  745. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_ch);
  746. } else if (strcmp(attr->name, "num_luns") == 0) {
  747. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_lun);
  748. } else if (strcmp(attr->name, "num_planes") == 0) {
  749. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pln);
  750. } else if (strcmp(attr->name, "num_blocks") == 0) { /* u16 */
  751. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_blk);
  752. } else if (strcmp(attr->name, "num_pages") == 0) {
  753. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pg);
  754. } else if (strcmp(attr->name, "page_size") == 0) {
  755. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fpg_sz);
  756. } else if (strcmp(attr->name, "hw_sector_size") == 0) {
  757. return scnprintf(page, PAGE_SIZE, "%u\n", grp->csecs);
  758. } else if (strcmp(attr->name, "oob_sector_size") == 0) {/* u32 */
  759. return scnprintf(page, PAGE_SIZE, "%u\n", grp->sos);
  760. } else if (strcmp(attr->name, "read_typ") == 0) {
  761. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdt);
  762. } else if (strcmp(attr->name, "read_max") == 0) {
  763. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdm);
  764. } else if (strcmp(attr->name, "prog_typ") == 0) {
  765. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprt);
  766. } else if (strcmp(attr->name, "prog_max") == 0) {
  767. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprm);
  768. } else if (strcmp(attr->name, "erase_typ") == 0) {
  769. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbet);
  770. } else if (strcmp(attr->name, "erase_max") == 0) {
  771. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbem);
  772. } else if (strcmp(attr->name, "multiplane_modes") == 0) {
  773. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mpos);
  774. } else if (strcmp(attr->name, "media_capabilities") == 0) {
  775. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mccap);
  776. } else if (strcmp(attr->name, "max_phys_secs") == 0) {
  777. return scnprintf(page, PAGE_SIZE, "%u\n",
  778. ndev->ops->max_phys_sect);
  779. } else {
  780. return scnprintf(page,
  781. PAGE_SIZE,
  782. "Unhandled attr(%s) in `nvm_dev_attr_show`\n",
  783. attr->name);
  784. }
  785. }
  786. #define NVM_DEV_ATTR_RO(_name) \
  787. DEVICE_ATTR(_name, S_IRUGO, nvm_dev_attr_show, NULL)
  788. static NVM_DEV_ATTR_RO(version);
  789. static NVM_DEV_ATTR_RO(vendor_opcode);
  790. static NVM_DEV_ATTR_RO(capabilities);
  791. static NVM_DEV_ATTR_RO(device_mode);
  792. static NVM_DEV_ATTR_RO(ppa_format);
  793. static NVM_DEV_ATTR_RO(media_manager);
  794. static NVM_DEV_ATTR_RO(media_type);
  795. static NVM_DEV_ATTR_RO(flash_media_type);
  796. static NVM_DEV_ATTR_RO(num_channels);
  797. static NVM_DEV_ATTR_RO(num_luns);
  798. static NVM_DEV_ATTR_RO(num_planes);
  799. static NVM_DEV_ATTR_RO(num_blocks);
  800. static NVM_DEV_ATTR_RO(num_pages);
  801. static NVM_DEV_ATTR_RO(page_size);
  802. static NVM_DEV_ATTR_RO(hw_sector_size);
  803. static NVM_DEV_ATTR_RO(oob_sector_size);
  804. static NVM_DEV_ATTR_RO(read_typ);
  805. static NVM_DEV_ATTR_RO(read_max);
  806. static NVM_DEV_ATTR_RO(prog_typ);
  807. static NVM_DEV_ATTR_RO(prog_max);
  808. static NVM_DEV_ATTR_RO(erase_typ);
  809. static NVM_DEV_ATTR_RO(erase_max);
  810. static NVM_DEV_ATTR_RO(multiplane_modes);
  811. static NVM_DEV_ATTR_RO(media_capabilities);
  812. static NVM_DEV_ATTR_RO(max_phys_secs);
  813. static struct attribute *nvm_dev_attrs[] = {
  814. &dev_attr_version.attr,
  815. &dev_attr_vendor_opcode.attr,
  816. &dev_attr_capabilities.attr,
  817. &dev_attr_device_mode.attr,
  818. &dev_attr_media_manager.attr,
  819. &dev_attr_ppa_format.attr,
  820. &dev_attr_media_type.attr,
  821. &dev_attr_flash_media_type.attr,
  822. &dev_attr_num_channels.attr,
  823. &dev_attr_num_luns.attr,
  824. &dev_attr_num_planes.attr,
  825. &dev_attr_num_blocks.attr,
  826. &dev_attr_num_pages.attr,
  827. &dev_attr_page_size.attr,
  828. &dev_attr_hw_sector_size.attr,
  829. &dev_attr_oob_sector_size.attr,
  830. &dev_attr_read_typ.attr,
  831. &dev_attr_read_max.attr,
  832. &dev_attr_prog_typ.attr,
  833. &dev_attr_prog_max.attr,
  834. &dev_attr_erase_typ.attr,
  835. &dev_attr_erase_max.attr,
  836. &dev_attr_multiplane_modes.attr,
  837. &dev_attr_media_capabilities.attr,
  838. &dev_attr_max_phys_secs.attr,
  839. NULL,
  840. };
  841. static const struct attribute_group nvm_dev_attr_group = {
  842. .name = "lightnvm",
  843. .attrs = nvm_dev_attrs,
  844. };
  845. int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  846. {
  847. return sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  848. &nvm_dev_attr_group);
  849. }
  850. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns)
  851. {
  852. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  853. &nvm_dev_attr_group);
  854. }
  855. /* move to shared place when used in multiple places. */
  856. #define PCI_VENDOR_ID_CNEX 0x1d1d
  857. #define PCI_DEVICE_ID_CNEX_WL 0x2807
  858. #define PCI_DEVICE_ID_CNEX_QEMU 0x1f1f
  859. int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
  860. {
  861. struct nvme_ctrl *ctrl = ns->ctrl;
  862. /* XXX: this is poking into PCI structures from generic code! */
  863. struct pci_dev *pdev = to_pci_dev(ctrl->dev);
  864. /* QEMU NVMe simulator - PCI ID + Vendor specific bit */
  865. if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
  866. pdev->device == PCI_DEVICE_ID_CNEX_QEMU &&
  867. id->vs[0] == 0x1)
  868. return 1;
  869. /* CNEX Labs - PCI ID + Vendor specific bit */
  870. if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
  871. pdev->device == PCI_DEVICE_ID_CNEX_WL &&
  872. id->vs[0] == 0x1)
  873. return 1;
  874. return 0;
  875. }