lightnvm.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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) != 4096);
  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, int error)
  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 -ENOMEM;
  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 void nvme_nvm_end_user_vio(struct request *rq, int error)
  493. {
  494. struct completion *waiting = rq->end_io_data;
  495. complete(waiting);
  496. }
  497. static int nvme_nvm_submit_user_cmd(struct request_queue *q,
  498. struct nvme_ns *ns,
  499. struct nvme_nvm_command *vcmd,
  500. void __user *ubuf, unsigned int bufflen,
  501. void __user *meta_buf, unsigned int meta_len,
  502. void __user *ppa_buf, unsigned int ppa_len,
  503. u32 *result, u64 *status, unsigned int timeout)
  504. {
  505. bool write = nvme_is_write((struct nvme_command *)vcmd);
  506. struct nvm_dev *dev = ns->ndev;
  507. struct gendisk *disk = ns->disk;
  508. struct request *rq;
  509. struct bio *bio = NULL;
  510. __le64 *ppa_list = NULL;
  511. dma_addr_t ppa_dma;
  512. __le64 *metadata = NULL;
  513. dma_addr_t metadata_dma;
  514. DECLARE_COMPLETION_ONSTACK(wait);
  515. int ret = 0;
  516. rq = nvme_alloc_request(q, (struct nvme_command *)vcmd, 0,
  517. NVME_QID_ANY);
  518. if (IS_ERR(rq)) {
  519. ret = -ENOMEM;
  520. goto err_cmd;
  521. }
  522. rq->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  523. rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
  524. rq->end_io_data = &wait;
  525. if (ppa_buf && ppa_len) {
  526. ppa_list = dma_pool_alloc(dev->dma_pool, GFP_KERNEL, &ppa_dma);
  527. if (!ppa_list) {
  528. ret = -ENOMEM;
  529. goto err_rq;
  530. }
  531. if (copy_from_user(ppa_list, (void __user *)ppa_buf,
  532. sizeof(u64) * (ppa_len + 1))) {
  533. ret = -EFAULT;
  534. goto err_ppa;
  535. }
  536. vcmd->ph_rw.spba = cpu_to_le64(ppa_dma);
  537. } else {
  538. vcmd->ph_rw.spba = cpu_to_le64((uintptr_t)ppa_buf);
  539. }
  540. if (ubuf && bufflen) {
  541. ret = blk_rq_map_user(q, rq, NULL, ubuf, bufflen, GFP_KERNEL);
  542. if (ret)
  543. goto err_ppa;
  544. bio = rq->bio;
  545. if (meta_buf && meta_len) {
  546. metadata = dma_pool_alloc(dev->dma_pool, GFP_KERNEL,
  547. &metadata_dma);
  548. if (!metadata) {
  549. ret = -ENOMEM;
  550. goto err_map;
  551. }
  552. if (write) {
  553. if (copy_from_user(metadata,
  554. (void __user *)meta_buf,
  555. meta_len)) {
  556. ret = -EFAULT;
  557. goto err_meta;
  558. }
  559. }
  560. vcmd->ph_rw.metadata = cpu_to_le64(metadata_dma);
  561. }
  562. if (!disk)
  563. goto submit;
  564. bio->bi_bdev = bdget_disk(disk, 0);
  565. if (!bio->bi_bdev) {
  566. ret = -ENODEV;
  567. goto err_meta;
  568. }
  569. }
  570. submit:
  571. blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_user_vio);
  572. wait_for_completion_io(&wait);
  573. if (nvme_req(rq)->flags & NVME_REQ_CANCELLED)
  574. ret = -EINTR;
  575. else if (nvme_req(rq)->status & 0x7ff)
  576. ret = -EIO;
  577. if (result)
  578. *result = nvme_req(rq)->status & 0x7ff;
  579. if (status)
  580. *status = le64_to_cpu(nvme_req(rq)->result.u64);
  581. if (metadata && !ret && !write) {
  582. if (copy_to_user(meta_buf, (void *)metadata, meta_len))
  583. ret = -EFAULT;
  584. }
  585. err_meta:
  586. if (meta_buf && meta_len)
  587. dma_pool_free(dev->dma_pool, metadata, metadata_dma);
  588. err_map:
  589. if (bio) {
  590. if (disk && bio->bi_bdev)
  591. bdput(bio->bi_bdev);
  592. blk_rq_unmap_user(bio);
  593. }
  594. err_ppa:
  595. if (ppa_buf && ppa_len)
  596. dma_pool_free(dev->dma_pool, ppa_list, ppa_dma);
  597. err_rq:
  598. blk_mq_free_request(rq);
  599. err_cmd:
  600. return ret;
  601. }
  602. static int nvme_nvm_submit_vio(struct nvme_ns *ns,
  603. struct nvm_user_vio __user *uvio)
  604. {
  605. struct nvm_user_vio vio;
  606. struct nvme_nvm_command c;
  607. unsigned int length;
  608. int ret;
  609. if (copy_from_user(&vio, uvio, sizeof(vio)))
  610. return -EFAULT;
  611. if (vio.flags)
  612. return -EINVAL;
  613. memset(&c, 0, sizeof(c));
  614. c.ph_rw.opcode = vio.opcode;
  615. c.ph_rw.nsid = cpu_to_le32(ns->ns_id);
  616. c.ph_rw.control = cpu_to_le16(vio.control);
  617. c.ph_rw.length = cpu_to_le16(vio.nppas);
  618. length = (vio.nppas + 1) << ns->lba_shift;
  619. ret = nvme_nvm_submit_user_cmd(ns->queue, ns, &c,
  620. (void __user *)(uintptr_t)vio.addr, length,
  621. (void __user *)(uintptr_t)vio.metadata,
  622. vio.metadata_len,
  623. (void __user *)(uintptr_t)vio.ppa_list, vio.nppas,
  624. &vio.result, &vio.status, 0);
  625. if (ret && copy_to_user(uvio, &vio, sizeof(vio)))
  626. return -EFAULT;
  627. return ret;
  628. }
  629. static int nvme_nvm_user_vcmd(struct nvme_ns *ns, int admin,
  630. struct nvm_passthru_vio __user *uvcmd)
  631. {
  632. struct nvm_passthru_vio vcmd;
  633. struct nvme_nvm_command c;
  634. struct request_queue *q;
  635. unsigned int timeout = 0;
  636. int ret;
  637. if (copy_from_user(&vcmd, uvcmd, sizeof(vcmd)))
  638. return -EFAULT;
  639. if ((vcmd.opcode != 0xF2) && (!capable(CAP_SYS_ADMIN)))
  640. return -EACCES;
  641. if (vcmd.flags)
  642. return -EINVAL;
  643. memset(&c, 0, sizeof(c));
  644. c.common.opcode = vcmd.opcode;
  645. c.common.nsid = cpu_to_le32(ns->ns_id);
  646. c.common.cdw2[0] = cpu_to_le32(vcmd.cdw2);
  647. c.common.cdw2[1] = cpu_to_le32(vcmd.cdw3);
  648. /* cdw11-12 */
  649. c.ph_rw.length = cpu_to_le16(vcmd.nppas);
  650. c.ph_rw.control = cpu_to_le16(vcmd.control);
  651. c.common.cdw10[3] = cpu_to_le32(vcmd.cdw13);
  652. c.common.cdw10[4] = cpu_to_le32(vcmd.cdw14);
  653. c.common.cdw10[5] = cpu_to_le32(vcmd.cdw15);
  654. if (vcmd.timeout_ms)
  655. timeout = msecs_to_jiffies(vcmd.timeout_ms);
  656. q = admin ? ns->ctrl->admin_q : ns->queue;
  657. ret = nvme_nvm_submit_user_cmd(q, ns,
  658. (struct nvme_nvm_command *)&c,
  659. (void __user *)(uintptr_t)vcmd.addr, vcmd.data_len,
  660. (void __user *)(uintptr_t)vcmd.metadata,
  661. vcmd.metadata_len,
  662. (void __user *)(uintptr_t)vcmd.ppa_list, vcmd.nppas,
  663. &vcmd.result, &vcmd.status, timeout);
  664. if (ret && copy_to_user(uvcmd, &vcmd, sizeof(vcmd)))
  665. return -EFAULT;
  666. return ret;
  667. }
  668. int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg)
  669. {
  670. switch (cmd) {
  671. case NVME_NVM_IOCTL_ADMIN_VIO:
  672. return nvme_nvm_user_vcmd(ns, 1, (void __user *)arg);
  673. case NVME_NVM_IOCTL_IO_VIO:
  674. return nvme_nvm_user_vcmd(ns, 0, (void __user *)arg);
  675. case NVME_NVM_IOCTL_SUBMIT_VIO:
  676. return nvme_nvm_submit_vio(ns, (void __user *)arg);
  677. default:
  678. return -ENOTTY;
  679. }
  680. }
  681. int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node)
  682. {
  683. struct request_queue *q = ns->queue;
  684. struct nvm_dev *dev;
  685. _nvme_nvm_check_size();
  686. dev = nvm_alloc_dev(node);
  687. if (!dev)
  688. return -ENOMEM;
  689. dev->q = q;
  690. memcpy(dev->name, disk_name, DISK_NAME_LEN);
  691. dev->ops = &nvme_nvm_dev_ops;
  692. dev->private_data = ns;
  693. ns->ndev = dev;
  694. return nvm_register(dev);
  695. }
  696. void nvme_nvm_unregister(struct nvme_ns *ns)
  697. {
  698. nvm_unregister(ns->ndev);
  699. }
  700. static ssize_t nvm_dev_attr_show(struct device *dev,
  701. struct device_attribute *dattr, char *page)
  702. {
  703. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  704. struct nvm_dev *ndev = ns->ndev;
  705. struct nvm_id *id;
  706. struct nvm_id_group *grp;
  707. struct attribute *attr;
  708. if (!ndev)
  709. return 0;
  710. id = &ndev->identity;
  711. grp = &id->grp;
  712. attr = &dattr->attr;
  713. if (strcmp(attr->name, "version") == 0) {
  714. return scnprintf(page, PAGE_SIZE, "%u\n", id->ver_id);
  715. } else if (strcmp(attr->name, "vendor_opcode") == 0) {
  716. return scnprintf(page, PAGE_SIZE, "%u\n", id->vmnt);
  717. } else if (strcmp(attr->name, "capabilities") == 0) {
  718. return scnprintf(page, PAGE_SIZE, "%u\n", id->cap);
  719. } else if (strcmp(attr->name, "device_mode") == 0) {
  720. return scnprintf(page, PAGE_SIZE, "%u\n", id->dom);
  721. /* kept for compatibility */
  722. } else if (strcmp(attr->name, "media_manager") == 0) {
  723. return scnprintf(page, PAGE_SIZE, "%s\n", "gennvm");
  724. } else if (strcmp(attr->name, "ppa_format") == 0) {
  725. return scnprintf(page, PAGE_SIZE,
  726. "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  727. id->ppaf.ch_offset, id->ppaf.ch_len,
  728. id->ppaf.lun_offset, id->ppaf.lun_len,
  729. id->ppaf.pln_offset, id->ppaf.pln_len,
  730. id->ppaf.blk_offset, id->ppaf.blk_len,
  731. id->ppaf.pg_offset, id->ppaf.pg_len,
  732. id->ppaf.sect_offset, id->ppaf.sect_len);
  733. } else if (strcmp(attr->name, "media_type") == 0) { /* u8 */
  734. return scnprintf(page, PAGE_SIZE, "%u\n", grp->mtype);
  735. } else if (strcmp(attr->name, "flash_media_type") == 0) {
  736. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fmtype);
  737. } else if (strcmp(attr->name, "num_channels") == 0) {
  738. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_ch);
  739. } else if (strcmp(attr->name, "num_luns") == 0) {
  740. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_lun);
  741. } else if (strcmp(attr->name, "num_planes") == 0) {
  742. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pln);
  743. } else if (strcmp(attr->name, "num_blocks") == 0) { /* u16 */
  744. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_blk);
  745. } else if (strcmp(attr->name, "num_pages") == 0) {
  746. return scnprintf(page, PAGE_SIZE, "%u\n", grp->num_pg);
  747. } else if (strcmp(attr->name, "page_size") == 0) {
  748. return scnprintf(page, PAGE_SIZE, "%u\n", grp->fpg_sz);
  749. } else if (strcmp(attr->name, "hw_sector_size") == 0) {
  750. return scnprintf(page, PAGE_SIZE, "%u\n", grp->csecs);
  751. } else if (strcmp(attr->name, "oob_sector_size") == 0) {/* u32 */
  752. return scnprintf(page, PAGE_SIZE, "%u\n", grp->sos);
  753. } else if (strcmp(attr->name, "read_typ") == 0) {
  754. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdt);
  755. } else if (strcmp(attr->name, "read_max") == 0) {
  756. return scnprintf(page, PAGE_SIZE, "%u\n", grp->trdm);
  757. } else if (strcmp(attr->name, "prog_typ") == 0) {
  758. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprt);
  759. } else if (strcmp(attr->name, "prog_max") == 0) {
  760. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tprm);
  761. } else if (strcmp(attr->name, "erase_typ") == 0) {
  762. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbet);
  763. } else if (strcmp(attr->name, "erase_max") == 0) {
  764. return scnprintf(page, PAGE_SIZE, "%u\n", grp->tbem);
  765. } else if (strcmp(attr->name, "multiplane_modes") == 0) {
  766. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mpos);
  767. } else if (strcmp(attr->name, "media_capabilities") == 0) {
  768. return scnprintf(page, PAGE_SIZE, "0x%08x\n", grp->mccap);
  769. } else if (strcmp(attr->name, "max_phys_secs") == 0) {
  770. return scnprintf(page, PAGE_SIZE, "%u\n",
  771. ndev->ops->max_phys_sect);
  772. } else {
  773. return scnprintf(page,
  774. PAGE_SIZE,
  775. "Unhandled attr(%s) in `nvm_dev_attr_show`\n",
  776. attr->name);
  777. }
  778. }
  779. #define NVM_DEV_ATTR_RO(_name) \
  780. DEVICE_ATTR(_name, S_IRUGO, nvm_dev_attr_show, NULL)
  781. static NVM_DEV_ATTR_RO(version);
  782. static NVM_DEV_ATTR_RO(vendor_opcode);
  783. static NVM_DEV_ATTR_RO(capabilities);
  784. static NVM_DEV_ATTR_RO(device_mode);
  785. static NVM_DEV_ATTR_RO(ppa_format);
  786. static NVM_DEV_ATTR_RO(media_manager);
  787. static NVM_DEV_ATTR_RO(media_type);
  788. static NVM_DEV_ATTR_RO(flash_media_type);
  789. static NVM_DEV_ATTR_RO(num_channels);
  790. static NVM_DEV_ATTR_RO(num_luns);
  791. static NVM_DEV_ATTR_RO(num_planes);
  792. static NVM_DEV_ATTR_RO(num_blocks);
  793. static NVM_DEV_ATTR_RO(num_pages);
  794. static NVM_DEV_ATTR_RO(page_size);
  795. static NVM_DEV_ATTR_RO(hw_sector_size);
  796. static NVM_DEV_ATTR_RO(oob_sector_size);
  797. static NVM_DEV_ATTR_RO(read_typ);
  798. static NVM_DEV_ATTR_RO(read_max);
  799. static NVM_DEV_ATTR_RO(prog_typ);
  800. static NVM_DEV_ATTR_RO(prog_max);
  801. static NVM_DEV_ATTR_RO(erase_typ);
  802. static NVM_DEV_ATTR_RO(erase_max);
  803. static NVM_DEV_ATTR_RO(multiplane_modes);
  804. static NVM_DEV_ATTR_RO(media_capabilities);
  805. static NVM_DEV_ATTR_RO(max_phys_secs);
  806. static struct attribute *nvm_dev_attrs[] = {
  807. &dev_attr_version.attr,
  808. &dev_attr_vendor_opcode.attr,
  809. &dev_attr_capabilities.attr,
  810. &dev_attr_device_mode.attr,
  811. &dev_attr_media_manager.attr,
  812. &dev_attr_ppa_format.attr,
  813. &dev_attr_media_type.attr,
  814. &dev_attr_flash_media_type.attr,
  815. &dev_attr_num_channels.attr,
  816. &dev_attr_num_luns.attr,
  817. &dev_attr_num_planes.attr,
  818. &dev_attr_num_blocks.attr,
  819. &dev_attr_num_pages.attr,
  820. &dev_attr_page_size.attr,
  821. &dev_attr_hw_sector_size.attr,
  822. &dev_attr_oob_sector_size.attr,
  823. &dev_attr_read_typ.attr,
  824. &dev_attr_read_max.attr,
  825. &dev_attr_prog_typ.attr,
  826. &dev_attr_prog_max.attr,
  827. &dev_attr_erase_typ.attr,
  828. &dev_attr_erase_max.attr,
  829. &dev_attr_multiplane_modes.attr,
  830. &dev_attr_media_capabilities.attr,
  831. &dev_attr_max_phys_secs.attr,
  832. NULL,
  833. };
  834. static const struct attribute_group nvm_dev_attr_group = {
  835. .name = "lightnvm",
  836. .attrs = nvm_dev_attrs,
  837. };
  838. int nvme_nvm_register_sysfs(struct nvme_ns *ns)
  839. {
  840. return sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  841. &nvm_dev_attr_group);
  842. }
  843. void nvme_nvm_unregister_sysfs(struct nvme_ns *ns)
  844. {
  845. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  846. &nvm_dev_attr_group);
  847. }
  848. /* move to shared place when used in multiple places. */
  849. #define PCI_VENDOR_ID_CNEX 0x1d1d
  850. #define PCI_DEVICE_ID_CNEX_WL 0x2807
  851. #define PCI_DEVICE_ID_CNEX_QEMU 0x1f1f
  852. int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
  853. {
  854. struct nvme_ctrl *ctrl = ns->ctrl;
  855. /* XXX: this is poking into PCI structures from generic code! */
  856. struct pci_dev *pdev = to_pci_dev(ctrl->dev);
  857. /* QEMU NVMe simulator - PCI ID + Vendor specific bit */
  858. if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
  859. pdev->device == PCI_DEVICE_ID_CNEX_QEMU &&
  860. id->vs[0] == 0x1)
  861. return 1;
  862. /* CNEX Labs - PCI ID + Vendor specific bit */
  863. if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
  864. pdev->device == PCI_DEVICE_ID_CNEX_WL &&
  865. id->vs[0] == 0x1)
  866. return 1;
  867. return 0;
  868. }