lightnvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. enum nvme_nvm_admin_opcode {
  28. nvme_nvm_admin_identity = 0xe2,
  29. nvme_nvm_admin_get_l2p_tbl = 0xea,
  30. nvme_nvm_admin_get_bb_tbl = 0xf2,
  31. nvme_nvm_admin_set_bb_tbl = 0xf1,
  32. };
  33. struct nvme_nvm_hb_rw {
  34. __u8 opcode;
  35. __u8 flags;
  36. __u16 command_id;
  37. __le32 nsid;
  38. __u64 rsvd2;
  39. __le64 metadata;
  40. __le64 prp1;
  41. __le64 prp2;
  42. __le64 spba;
  43. __le16 length;
  44. __le16 control;
  45. __le32 dsmgmt;
  46. __le64 slba;
  47. };
  48. struct nvme_nvm_ph_rw {
  49. __u8 opcode;
  50. __u8 flags;
  51. __u16 command_id;
  52. __le32 nsid;
  53. __u64 rsvd2;
  54. __le64 metadata;
  55. __le64 prp1;
  56. __le64 prp2;
  57. __le64 spba;
  58. __le16 length;
  59. __le16 control;
  60. __le32 dsmgmt;
  61. __le64 resv;
  62. };
  63. struct nvme_nvm_identity {
  64. __u8 opcode;
  65. __u8 flags;
  66. __u16 command_id;
  67. __le32 nsid;
  68. __u64 rsvd[2];
  69. __le64 prp1;
  70. __le64 prp2;
  71. __le32 chnl_off;
  72. __u32 rsvd11[5];
  73. };
  74. struct nvme_nvm_l2ptbl {
  75. __u8 opcode;
  76. __u8 flags;
  77. __u16 command_id;
  78. __le32 nsid;
  79. __le32 cdw2[4];
  80. __le64 prp1;
  81. __le64 prp2;
  82. __le64 slba;
  83. __le32 nlb;
  84. __le16 cdw14[6];
  85. };
  86. struct nvme_nvm_getbbtbl {
  87. __u8 opcode;
  88. __u8 flags;
  89. __u16 command_id;
  90. __le32 nsid;
  91. __u64 rsvd[2];
  92. __le64 prp1;
  93. __le64 prp2;
  94. __le64 spba;
  95. __u32 rsvd4[4];
  96. };
  97. struct nvme_nvm_setbbtbl {
  98. __u8 opcode;
  99. __u8 flags;
  100. __u16 command_id;
  101. __le32 nsid;
  102. __le64 rsvd[2];
  103. __le64 prp1;
  104. __le64 prp2;
  105. __le64 spba;
  106. __le16 nlb;
  107. __u8 value;
  108. __u8 rsvd3;
  109. __u32 rsvd4[3];
  110. };
  111. struct nvme_nvm_erase_blk {
  112. __u8 opcode;
  113. __u8 flags;
  114. __u16 command_id;
  115. __le32 nsid;
  116. __u64 rsvd[2];
  117. __le64 prp1;
  118. __le64 prp2;
  119. __le64 spba;
  120. __le16 length;
  121. __le16 control;
  122. __le32 dsmgmt;
  123. __le64 resv;
  124. };
  125. struct nvme_nvm_command {
  126. union {
  127. struct nvme_common_command common;
  128. struct nvme_nvm_identity identity;
  129. struct nvme_nvm_hb_rw hb_rw;
  130. struct nvme_nvm_ph_rw ph_rw;
  131. struct nvme_nvm_l2ptbl l2p;
  132. struct nvme_nvm_getbbtbl get_bb;
  133. struct nvme_nvm_setbbtbl set_bb;
  134. struct nvme_nvm_erase_blk erase;
  135. };
  136. };
  137. struct nvme_nvm_completion {
  138. __le64 result; /* Used by LightNVM to return ppa completions */
  139. __le16 sq_head; /* how much of this queue may be reclaimed */
  140. __le16 sq_id; /* submission queue that generated this entry */
  141. __u16 command_id; /* of the command which completed */
  142. __le16 status; /* did the command fail, and if so, why? */
  143. };
  144. #define NVME_NVM_LP_MLC_PAIRS 886
  145. struct nvme_nvm_lp_mlc {
  146. __le16 num_pairs;
  147. __u8 pairs[NVME_NVM_LP_MLC_PAIRS];
  148. };
  149. struct nvme_nvm_lp_tbl {
  150. __u8 id[8];
  151. struct nvme_nvm_lp_mlc mlc;
  152. };
  153. struct nvme_nvm_id_group {
  154. __u8 mtype;
  155. __u8 fmtype;
  156. __le16 res16;
  157. __u8 num_ch;
  158. __u8 num_lun;
  159. __u8 num_pln;
  160. __u8 rsvd1;
  161. __le16 num_blk;
  162. __le16 num_pg;
  163. __le16 fpg_sz;
  164. __le16 csecs;
  165. __le16 sos;
  166. __le16 rsvd2;
  167. __le32 trdt;
  168. __le32 trdm;
  169. __le32 tprt;
  170. __le32 tprm;
  171. __le32 tbet;
  172. __le32 tbem;
  173. __le32 mpos;
  174. __le32 mccap;
  175. __le16 cpar;
  176. __u8 reserved[10];
  177. struct nvme_nvm_lp_tbl lptbl;
  178. } __packed;
  179. struct nvme_nvm_addr_format {
  180. __u8 ch_offset;
  181. __u8 ch_len;
  182. __u8 lun_offset;
  183. __u8 lun_len;
  184. __u8 pln_offset;
  185. __u8 pln_len;
  186. __u8 blk_offset;
  187. __u8 blk_len;
  188. __u8 pg_offset;
  189. __u8 pg_len;
  190. __u8 sect_offset;
  191. __u8 sect_len;
  192. __u8 res[4];
  193. } __packed;
  194. struct nvme_nvm_id {
  195. __u8 ver_id;
  196. __u8 vmnt;
  197. __u8 cgrps;
  198. __u8 res;
  199. __le32 cap;
  200. __le32 dom;
  201. struct nvme_nvm_addr_format ppaf;
  202. __u8 resv[228];
  203. struct nvme_nvm_id_group groups[4];
  204. } __packed;
  205. struct nvme_nvm_bb_tbl {
  206. __u8 tblid[4];
  207. __le16 verid;
  208. __le16 revid;
  209. __le32 rvsd1;
  210. __le32 tblks;
  211. __le32 tfact;
  212. __le32 tgrown;
  213. __le32 tdresv;
  214. __le32 thresv;
  215. __le32 rsvd2[8];
  216. __u8 blk[0];
  217. };
  218. /*
  219. * Check we didn't inadvertently grow the command struct
  220. */
  221. static inline void _nvme_nvm_check_size(void)
  222. {
  223. BUILD_BUG_ON(sizeof(struct nvme_nvm_identity) != 64);
  224. BUILD_BUG_ON(sizeof(struct nvme_nvm_hb_rw) != 64);
  225. BUILD_BUG_ON(sizeof(struct nvme_nvm_ph_rw) != 64);
  226. BUILD_BUG_ON(sizeof(struct nvme_nvm_getbbtbl) != 64);
  227. BUILD_BUG_ON(sizeof(struct nvme_nvm_setbbtbl) != 64);
  228. BUILD_BUG_ON(sizeof(struct nvme_nvm_l2ptbl) != 64);
  229. BUILD_BUG_ON(sizeof(struct nvme_nvm_erase_blk) != 64);
  230. BUILD_BUG_ON(sizeof(struct nvme_nvm_id_group) != 960);
  231. BUILD_BUG_ON(sizeof(struct nvme_nvm_addr_format) != 128);
  232. BUILD_BUG_ON(sizeof(struct nvme_nvm_id) != 4096);
  233. BUILD_BUG_ON(sizeof(struct nvme_nvm_bb_tbl) != 512);
  234. }
  235. static int init_grps(struct nvm_id *nvm_id, struct nvme_nvm_id *nvme_nvm_id)
  236. {
  237. struct nvme_nvm_id_group *src;
  238. struct nvm_id_group *dst;
  239. int i, end;
  240. end = min_t(u32, 4, nvm_id->cgrps);
  241. for (i = 0; i < end; i++) {
  242. src = &nvme_nvm_id->groups[i];
  243. dst = &nvm_id->groups[i];
  244. dst->mtype = src->mtype;
  245. dst->fmtype = src->fmtype;
  246. dst->num_ch = src->num_ch;
  247. dst->num_lun = src->num_lun;
  248. dst->num_pln = src->num_pln;
  249. dst->num_pg = le16_to_cpu(src->num_pg);
  250. dst->num_blk = le16_to_cpu(src->num_blk);
  251. dst->fpg_sz = le16_to_cpu(src->fpg_sz);
  252. dst->csecs = le16_to_cpu(src->csecs);
  253. dst->sos = le16_to_cpu(src->sos);
  254. dst->trdt = le32_to_cpu(src->trdt);
  255. dst->trdm = le32_to_cpu(src->trdm);
  256. dst->tprt = le32_to_cpu(src->tprt);
  257. dst->tprm = le32_to_cpu(src->tprm);
  258. dst->tbet = le32_to_cpu(src->tbet);
  259. dst->tbem = le32_to_cpu(src->tbem);
  260. dst->mpos = le32_to_cpu(src->mpos);
  261. dst->mccap = le32_to_cpu(src->mccap);
  262. dst->cpar = le16_to_cpu(src->cpar);
  263. if (dst->fmtype == NVM_ID_FMTYPE_MLC) {
  264. memcpy(dst->lptbl.id, src->lptbl.id, 8);
  265. dst->lptbl.mlc.num_pairs =
  266. le16_to_cpu(src->lptbl.mlc.num_pairs);
  267. if (dst->lptbl.mlc.num_pairs > NVME_NVM_LP_MLC_PAIRS) {
  268. pr_err("nvm: number of MLC pairs not supported\n");
  269. return -EINVAL;
  270. }
  271. memcpy(dst->lptbl.mlc.pairs, src->lptbl.mlc.pairs,
  272. dst->lptbl.mlc.num_pairs);
  273. }
  274. }
  275. return 0;
  276. }
  277. static int nvme_nvm_identity(struct nvm_dev *nvmdev, struct nvm_id *nvm_id)
  278. {
  279. struct nvme_ns *ns = nvmdev->q->queuedata;
  280. struct nvme_nvm_id *nvme_nvm_id;
  281. struct nvme_nvm_command c = {};
  282. int ret;
  283. c.identity.opcode = nvme_nvm_admin_identity;
  284. c.identity.nsid = cpu_to_le32(ns->ns_id);
  285. c.identity.chnl_off = 0;
  286. nvme_nvm_id = kmalloc(sizeof(struct nvme_nvm_id), GFP_KERNEL);
  287. if (!nvme_nvm_id)
  288. return -ENOMEM;
  289. ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c,
  290. nvme_nvm_id, sizeof(struct nvme_nvm_id));
  291. if (ret) {
  292. ret = -EIO;
  293. goto out;
  294. }
  295. nvm_id->ver_id = nvme_nvm_id->ver_id;
  296. nvm_id->vmnt = nvme_nvm_id->vmnt;
  297. nvm_id->cgrps = nvme_nvm_id->cgrps;
  298. nvm_id->cap = le32_to_cpu(nvme_nvm_id->cap);
  299. nvm_id->dom = le32_to_cpu(nvme_nvm_id->dom);
  300. memcpy(&nvm_id->ppaf, &nvme_nvm_id->ppaf,
  301. sizeof(struct nvme_nvm_addr_format));
  302. ret = init_grps(nvm_id, nvme_nvm_id);
  303. out:
  304. kfree(nvme_nvm_id);
  305. return ret;
  306. }
  307. static int nvme_nvm_get_l2p_tbl(struct nvm_dev *nvmdev, u64 slba, u32 nlb,
  308. nvm_l2p_update_fn *update_l2p, void *priv)
  309. {
  310. struct nvme_ns *ns = nvmdev->q->queuedata;
  311. struct nvme_nvm_command c = {};
  312. u32 len = queue_max_hw_sectors(ns->ctrl->admin_q) << 9;
  313. u32 nlb_pr_rq = len / sizeof(u64);
  314. u64 cmd_slba = slba;
  315. void *entries;
  316. int ret = 0;
  317. c.l2p.opcode = nvme_nvm_admin_get_l2p_tbl;
  318. c.l2p.nsid = cpu_to_le32(ns->ns_id);
  319. entries = kmalloc(len, GFP_KERNEL);
  320. if (!entries)
  321. return -ENOMEM;
  322. while (nlb) {
  323. u32 cmd_nlb = min(nlb_pr_rq, nlb);
  324. c.l2p.slba = cpu_to_le64(cmd_slba);
  325. c.l2p.nlb = cpu_to_le32(cmd_nlb);
  326. ret = nvme_submit_sync_cmd(ns->ctrl->admin_q,
  327. (struct nvme_command *)&c, entries, len);
  328. if (ret) {
  329. dev_err(ns->ctrl->device,
  330. "L2P table transfer failed (%d)\n", ret);
  331. ret = -EIO;
  332. goto out;
  333. }
  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 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 = nvmdev->blks_per_lun * nvmdev->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, nvmdev->blks_per_lun * nvmdev->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. struct nvme_nvm_completion *cqe = rq->special;
  427. if (cqe)
  428. rqd->ppa_status = le64_to_cpu(cqe->result);
  429. nvm_end_io(rqd, error);
  430. kfree(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. rq = blk_mq_alloc_request(q, bio_data_dir(bio), 0);
  441. if (IS_ERR(rq))
  442. return -ENOMEM;
  443. cmd = kzalloc(sizeof(struct nvme_nvm_command) +
  444. sizeof(struct nvme_nvm_completion), GFP_KERNEL);
  445. if (!cmd) {
  446. blk_mq_free_request(rq);
  447. return -ENOMEM;
  448. }
  449. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  450. rq->ioprio = bio_prio(bio);
  451. if (bio_has_data(bio))
  452. rq->nr_phys_segments = bio_phys_segments(q, bio);
  453. rq->__data_len = bio->bi_iter.bi_size;
  454. rq->bio = rq->biotail = bio;
  455. nvme_nvm_rqtocmd(rq, rqd, ns, cmd);
  456. rq->cmd = (unsigned char *)cmd;
  457. rq->cmd_len = sizeof(struct nvme_nvm_command);
  458. rq->special = cmd + 1;
  459. rq->end_io_data = rqd;
  460. blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io);
  461. return 0;
  462. }
  463. static int nvme_nvm_erase_block(struct nvm_dev *dev, struct nvm_rq *rqd)
  464. {
  465. struct request_queue *q = dev->q;
  466. struct nvme_ns *ns = q->queuedata;
  467. struct nvme_nvm_command c = {};
  468. c.erase.opcode = NVM_OP_ERASE;
  469. c.erase.nsid = cpu_to_le32(ns->ns_id);
  470. c.erase.spba = cpu_to_le64(rqd->ppa_addr.ppa);
  471. c.erase.length = cpu_to_le16(rqd->nr_ppas - 1);
  472. return nvme_submit_sync_cmd(q, (struct nvme_command *)&c, NULL, 0);
  473. }
  474. static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name)
  475. {
  476. struct nvme_ns *ns = nvmdev->q->queuedata;
  477. return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0);
  478. }
  479. static void nvme_nvm_destroy_dma_pool(void *pool)
  480. {
  481. struct dma_pool *dma_pool = pool;
  482. dma_pool_destroy(dma_pool);
  483. }
  484. static void *nvme_nvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  485. gfp_t mem_flags, dma_addr_t *dma_handler)
  486. {
  487. return dma_pool_alloc(pool, mem_flags, dma_handler);
  488. }
  489. static void nvme_nvm_dev_dma_free(void *pool, void *addr,
  490. dma_addr_t dma_handler)
  491. {
  492. dma_pool_free(pool, addr, dma_handler);
  493. }
  494. static struct nvm_dev_ops nvme_nvm_dev_ops = {
  495. .identity = nvme_nvm_identity,
  496. .get_l2p_tbl = nvme_nvm_get_l2p_tbl,
  497. .get_bb_tbl = nvme_nvm_get_bb_tbl,
  498. .set_bb_tbl = nvme_nvm_set_bb_tbl,
  499. .submit_io = nvme_nvm_submit_io,
  500. .erase_block = nvme_nvm_erase_block,
  501. .create_dma_pool = nvme_nvm_create_dma_pool,
  502. .destroy_dma_pool = nvme_nvm_destroy_dma_pool,
  503. .dev_dma_alloc = nvme_nvm_dev_dma_alloc,
  504. .dev_dma_free = nvme_nvm_dev_dma_free,
  505. .max_phys_sect = 64,
  506. };
  507. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node,
  508. const struct attribute_group *attrs)
  509. {
  510. struct request_queue *q = ns->queue;
  511. struct nvm_dev *dev;
  512. int ret;
  513. dev = nvm_alloc_dev(node);
  514. if (!dev)
  515. return -ENOMEM;
  516. dev->q = q;
  517. memcpy(dev->name, disk_name, DISK_NAME_LEN);
  518. dev->ops = &nvme_nvm_dev_ops;
  519. dev->parent_dev = ns->ctrl->device;
  520. dev->private_data = ns;
  521. ns->ndev = dev;
  522. ret = nvm_register(dev);
  523. ns->lba_shift = ilog2(dev->sec_size);
  524. if (sysfs_create_group(&dev->dev.kobj, attrs))
  525. pr_warn("%s: failed to create sysfs group for identification\n",
  526. disk_name);
  527. return ret;
  528. }
  529. void nvme_nvm_unregister(struct nvme_ns *ns)
  530. {
  531. nvm_unregister(ns->ndev);
  532. }
  533. /* move to shared place when used in multiple places. */
  534. #define PCI_VENDOR_ID_CNEX 0x1d1d
  535. #define PCI_DEVICE_ID_CNEX_WL 0x2807
  536. #define PCI_DEVICE_ID_CNEX_QEMU 0x1f1f
  537. int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
  538. {
  539. struct nvme_ctrl *ctrl = ns->ctrl;
  540. /* XXX: this is poking into PCI structures from generic code! */
  541. struct pci_dev *pdev = to_pci_dev(ctrl->dev);
  542. /* QEMU NVMe simulator - PCI ID + Vendor specific bit */
  543. if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
  544. pdev->device == PCI_DEVICE_ID_CNEX_QEMU &&
  545. id->vs[0] == 0x1)
  546. return 1;
  547. /* CNEX Labs - PCI ID + Vendor specific bit */
  548. if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
  549. pdev->device == PCI_DEVICE_ID_CNEX_WL &&
  550. id->vs[0] == 0x1)
  551. return 1;
  552. return 0;
  553. }