blob: 42a01a93198944717f2f0266aaee7570d0c52467 [file] [log] [blame]
Matias Bjørlingca064082015-10-29 17:57:29 +09001/*
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
23#include "nvme.h"
24
Matias Bjørlingca064082015-10-29 17:57:29 +090025#include <linux/nvme.h>
26#include <linux/bitops.h>
27#include <linux/lightnvm.h>
28#include <linux/vmalloc.h>
29
30enum nvme_nvm_admin_opcode {
31 nvme_nvm_admin_identity = 0xe2,
32 nvme_nvm_admin_get_l2p_tbl = 0xea,
33 nvme_nvm_admin_get_bb_tbl = 0xf2,
34 nvme_nvm_admin_set_bb_tbl = 0xf1,
35};
36
37struct nvme_nvm_hb_rw {
38 __u8 opcode;
39 __u8 flags;
40 __u16 command_id;
41 __le32 nsid;
42 __u64 rsvd2;
43 __le64 metadata;
44 __le64 prp1;
45 __le64 prp2;
46 __le64 spba;
47 __le16 length;
48 __le16 control;
49 __le32 dsmgmt;
50 __le64 slba;
51};
52
53struct nvme_nvm_ph_rw {
54 __u8 opcode;
55 __u8 flags;
56 __u16 command_id;
57 __le32 nsid;
58 __u64 rsvd2;
59 __le64 metadata;
60 __le64 prp1;
61 __le64 prp2;
62 __le64 spba;
63 __le16 length;
64 __le16 control;
65 __le32 dsmgmt;
66 __le64 resv;
67};
68
69struct nvme_nvm_identity {
70 __u8 opcode;
71 __u8 flags;
72 __u16 command_id;
73 __le32 nsid;
74 __u64 rsvd[2];
75 __le64 prp1;
76 __le64 prp2;
77 __le32 chnl_off;
78 __u32 rsvd11[5];
79};
80
81struct nvme_nvm_l2ptbl {
82 __u8 opcode;
83 __u8 flags;
84 __u16 command_id;
85 __le32 nsid;
86 __le32 cdw2[4];
87 __le64 prp1;
88 __le64 prp2;
89 __le64 slba;
90 __le32 nlb;
91 __le16 cdw14[6];
92};
93
Matias Bjørling11450462015-11-16 15:34:37 +010094struct nvme_nvm_getbbtbl {
Matias Bjørlingca064082015-10-29 17:57:29 +090095 __u8 opcode;
96 __u8 flags;
97 __u16 command_id;
98 __le32 nsid;
99 __u64 rsvd[2];
100 __le64 prp1;
101 __le64 prp2;
Matias Bjørling11450462015-11-16 15:34:37 +0100102 __le64 spba;
103 __u32 rsvd4[4];
104};
105
106struct nvme_nvm_setbbtbl {
107 __u8 opcode;
108 __u8 flags;
109 __u16 command_id;
110 __le32 nsid;
111 __le64 rsvd[2];
112 __le64 prp1;
113 __le64 prp2;
114 __le64 spba;
115 __le16 nlb;
116 __u8 value;
117 __u8 rsvd3;
118 __u32 rsvd4[3];
Matias Bjørlingca064082015-10-29 17:57:29 +0900119};
120
121struct nvme_nvm_erase_blk {
122 __u8 opcode;
123 __u8 flags;
124 __u16 command_id;
125 __le32 nsid;
126 __u64 rsvd[2];
127 __le64 prp1;
128 __le64 prp2;
129 __le64 spba;
130 __le16 length;
131 __le16 control;
132 __le32 dsmgmt;
133 __le64 resv;
134};
135
136struct nvme_nvm_command {
137 union {
138 struct nvme_common_command common;
139 struct nvme_nvm_identity identity;
140 struct nvme_nvm_hb_rw hb_rw;
141 struct nvme_nvm_ph_rw ph_rw;
142 struct nvme_nvm_l2ptbl l2p;
Matias Bjørling11450462015-11-16 15:34:37 +0100143 struct nvme_nvm_getbbtbl get_bb;
144 struct nvme_nvm_setbbtbl set_bb;
Matias Bjørlingca064082015-10-29 17:57:29 +0900145 struct nvme_nvm_erase_blk erase;
146 };
147};
148
Matias Bjørling6dde1d62016-02-04 15:13:26 +0100149#define NVME_NVM_LP_MLC_PAIRS 886
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100150struct nvme_nvm_lp_mlc {
151 __u16 num_pairs;
Matias Bjørling6dde1d62016-02-04 15:13:26 +0100152 __u8 pairs[NVME_NVM_LP_MLC_PAIRS];
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100153};
154
155struct nvme_nvm_lp_tbl {
156 __u8 id[8];
157 struct nvme_nvm_lp_mlc mlc;
158};
159
Matias Bjørlingca064082015-10-29 17:57:29 +0900160struct nvme_nvm_id_group {
161 __u8 mtype;
162 __u8 fmtype;
163 __le16 res16;
164 __u8 num_ch;
165 __u8 num_lun;
166 __u8 num_pln;
Matias Bjørling36d5dbc2015-11-16 15:34:38 +0100167 __u8 rsvd1;
Matias Bjørlingca064082015-10-29 17:57:29 +0900168 __le16 num_blk;
169 __le16 num_pg;
170 __le16 fpg_sz;
171 __le16 csecs;
172 __le16 sos;
Matias Bjørling36d5dbc2015-11-16 15:34:38 +0100173 __le16 rsvd2;
Matias Bjørlingca064082015-10-29 17:57:29 +0900174 __le32 trdt;
175 __le32 trdm;
176 __le32 tprt;
177 __le32 tprm;
178 __le32 tbet;
179 __le32 tbem;
180 __le32 mpos;
Matias Bjørling12be5ed2015-11-16 15:34:39 +0100181 __le32 mccap;
Matias Bjørlingca064082015-10-29 17:57:29 +0900182 __le16 cpar;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100183 __u8 reserved[10];
184 struct nvme_nvm_lp_tbl lptbl;
Matias Bjørlingca064082015-10-29 17:57:29 +0900185} __packed;
186
187struct nvme_nvm_addr_format {
188 __u8 ch_offset;
189 __u8 ch_len;
190 __u8 lun_offset;
191 __u8 lun_len;
192 __u8 pln_offset;
193 __u8 pln_len;
194 __u8 blk_offset;
195 __u8 blk_len;
196 __u8 pg_offset;
197 __u8 pg_len;
198 __u8 sect_offset;
199 __u8 sect_len;
200 __u8 res[4];
201} __packed;
202
203struct nvme_nvm_id {
204 __u8 ver_id;
205 __u8 vmnt;
206 __u8 cgrps;
Matias Bjørlingdad1b002015-11-16 15:34:46 +0100207 __u8 res;
Matias Bjørlingca064082015-10-29 17:57:29 +0900208 __le32 cap;
209 __le32 dom;
210 struct nvme_nvm_addr_format ppaf;
Matias Bjørlingdad1b002015-11-16 15:34:46 +0100211 __u8 resv[228];
Matias Bjørlingca064082015-10-29 17:57:29 +0900212 struct nvme_nvm_id_group groups[4];
213} __packed;
214
Matias Bjørling11450462015-11-16 15:34:37 +0100215struct nvme_nvm_bb_tbl {
216 __u8 tblid[4];
217 __le16 verid;
218 __le16 revid;
219 __le32 rvsd1;
220 __le32 tblks;
221 __le32 tfact;
222 __le32 tgrown;
223 __le32 tdresv;
224 __le32 thresv;
225 __le32 rsvd2[8];
226 __u8 blk[0];
227};
228
Matias Bjørlingca064082015-10-29 17:57:29 +0900229/*
230 * Check we didn't inadvertently grow the command struct
231 */
232static inline void _nvme_nvm_check_size(void)
233{
234 BUILD_BUG_ON(sizeof(struct nvme_nvm_identity) != 64);
235 BUILD_BUG_ON(sizeof(struct nvme_nvm_hb_rw) != 64);
236 BUILD_BUG_ON(sizeof(struct nvme_nvm_ph_rw) != 64);
Matias Bjørling11450462015-11-16 15:34:37 +0100237 BUILD_BUG_ON(sizeof(struct nvme_nvm_getbbtbl) != 64);
238 BUILD_BUG_ON(sizeof(struct nvme_nvm_setbbtbl) != 64);
Matias Bjørlingca064082015-10-29 17:57:29 +0900239 BUILD_BUG_ON(sizeof(struct nvme_nvm_l2ptbl) != 64);
240 BUILD_BUG_ON(sizeof(struct nvme_nvm_erase_blk) != 64);
241 BUILD_BUG_ON(sizeof(struct nvme_nvm_id_group) != 960);
242 BUILD_BUG_ON(sizeof(struct nvme_nvm_addr_format) != 128);
243 BUILD_BUG_ON(sizeof(struct nvme_nvm_id) != 4096);
Matias Bjørling11450462015-11-16 15:34:37 +0100244 BUILD_BUG_ON(sizeof(struct nvme_nvm_bb_tbl) != 512);
Matias Bjørlingca064082015-10-29 17:57:29 +0900245}
246
247static int init_grps(struct nvm_id *nvm_id, struct nvme_nvm_id *nvme_nvm_id)
248{
249 struct nvme_nvm_id_group *src;
250 struct nvm_id_group *dst;
251 int i, end;
252
253 end = min_t(u32, 4, nvm_id->cgrps);
254
255 for (i = 0; i < end; i++) {
256 src = &nvme_nvm_id->groups[i];
257 dst = &nvm_id->groups[i];
258
259 dst->mtype = src->mtype;
260 dst->fmtype = src->fmtype;
261 dst->num_ch = src->num_ch;
262 dst->num_lun = src->num_lun;
263 dst->num_pln = src->num_pln;
264
265 dst->num_pg = le16_to_cpu(src->num_pg);
266 dst->num_blk = le16_to_cpu(src->num_blk);
267 dst->fpg_sz = le16_to_cpu(src->fpg_sz);
268 dst->csecs = le16_to_cpu(src->csecs);
269 dst->sos = le16_to_cpu(src->sos);
270
271 dst->trdt = le32_to_cpu(src->trdt);
272 dst->trdm = le32_to_cpu(src->trdm);
273 dst->tprt = le32_to_cpu(src->tprt);
274 dst->tprm = le32_to_cpu(src->tprm);
275 dst->tbet = le32_to_cpu(src->tbet);
276 dst->tbem = le32_to_cpu(src->tbem);
277 dst->mpos = le32_to_cpu(src->mpos);
Matias Bjørling12be5ed2015-11-16 15:34:39 +0100278 dst->mccap = le32_to_cpu(src->mccap);
Matias Bjørlingca064082015-10-29 17:57:29 +0900279
280 dst->cpar = le16_to_cpu(src->cpar);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100281
282 if (dst->fmtype == NVM_ID_FMTYPE_MLC) {
283 memcpy(dst->lptbl.id, src->lptbl.id, 8);
284 dst->lptbl.mlc.num_pairs =
285 le16_to_cpu(src->lptbl.mlc.num_pairs);
Matias Bjørling6dde1d62016-02-04 15:13:26 +0100286
287 if (dst->lptbl.mlc.num_pairs > NVME_NVM_LP_MLC_PAIRS) {
288 pr_err("nvm: number of MLC pairs not supported\n");
289 return -EINVAL;
290 }
291
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100292 memcpy(dst->lptbl.mlc.pairs, src->lptbl.mlc.pairs,
Matias Bjørling6dde1d62016-02-04 15:13:26 +0100293 dst->lptbl.mlc.num_pairs);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100294 }
Matias Bjørlingca064082015-10-29 17:57:29 +0900295 }
296
297 return 0;
298}
299
Matias Bjørling16f26c32015-12-06 11:25:48 +0100300static int nvme_nvm_identity(struct nvm_dev *nvmdev, struct nvm_id *nvm_id)
Matias Bjørlingca064082015-10-29 17:57:29 +0900301{
Matias Bjørling16f26c32015-12-06 11:25:48 +0100302 struct nvme_ns *ns = nvmdev->q->queuedata;
Matias Bjørlingca064082015-10-29 17:57:29 +0900303 struct nvme_nvm_id *nvme_nvm_id;
304 struct nvme_nvm_command c = {};
305 int ret;
306
307 c.identity.opcode = nvme_nvm_admin_identity;
308 c.identity.nsid = cpu_to_le32(ns->ns_id);
309 c.identity.chnl_off = 0;
310
311 nvme_nvm_id = kmalloc(sizeof(struct nvme_nvm_id), GFP_KERNEL);
312 if (!nvme_nvm_id)
313 return -ENOMEM;
314
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700315 ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c,
Wenwei Tao47b31152015-11-20 13:47:55 +0100316 nvme_nvm_id, sizeof(struct nvme_nvm_id));
Matias Bjørlingca064082015-10-29 17:57:29 +0900317 if (ret) {
318 ret = -EIO;
319 goto out;
320 }
321
322 nvm_id->ver_id = nvme_nvm_id->ver_id;
323 nvm_id->vmnt = nvme_nvm_id->vmnt;
324 nvm_id->cgrps = nvme_nvm_id->cgrps;
325 nvm_id->cap = le32_to_cpu(nvme_nvm_id->cap);
326 nvm_id->dom = le32_to_cpu(nvme_nvm_id->dom);
Matias Bjørling2393bd32015-11-16 15:34:45 +0100327 memcpy(&nvm_id->ppaf, &nvme_nvm_id->ppaf,
328 sizeof(struct nvme_nvm_addr_format));
Matias Bjørlingca064082015-10-29 17:57:29 +0900329
330 ret = init_grps(nvm_id, nvme_nvm_id);
331out:
332 kfree(nvme_nvm_id);
333 return ret;
334}
335
Matias Bjørling16f26c32015-12-06 11:25:48 +0100336static int nvme_nvm_get_l2p_tbl(struct nvm_dev *nvmdev, u64 slba, u32 nlb,
Matias Bjørlingca064082015-10-29 17:57:29 +0900337 nvm_l2p_update_fn *update_l2p, void *priv)
338{
Matias Bjørling16f26c32015-12-06 11:25:48 +0100339 struct nvme_ns *ns = nvmdev->q->queuedata;
Matias Bjørlingca064082015-10-29 17:57:29 +0900340 struct nvme_nvm_command c = {};
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700341 u32 len = queue_max_hw_sectors(ns->ctrl->admin_q) << 9;
Dan Carpenter5f436e52015-11-04 01:37:31 +0300342 u32 nlb_pr_rq = len / sizeof(u64);
Matias Bjørlingca064082015-10-29 17:57:29 +0900343 u64 cmd_slba = slba;
344 void *entries;
345 int ret = 0;
346
347 c.l2p.opcode = nvme_nvm_admin_get_l2p_tbl;
348 c.l2p.nsid = cpu_to_le32(ns->ns_id);
349 entries = kmalloc(len, GFP_KERNEL);
350 if (!entries)
351 return -ENOMEM;
352
353 while (nlb) {
Dan Carpenter5f436e52015-11-04 01:37:31 +0300354 u32 cmd_nlb = min(nlb_pr_rq, nlb);
Matias Bjørlingca064082015-10-29 17:57:29 +0900355
356 c.l2p.slba = cpu_to_le64(cmd_slba);
357 c.l2p.nlb = cpu_to_le32(cmd_nlb);
358
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700359 ret = nvme_submit_sync_cmd(ns->ctrl->admin_q,
Wenwei Tao47b31152015-11-20 13:47:55 +0100360 (struct nvme_command *)&c, entries, len);
Matias Bjørlingca064082015-10-29 17:57:29 +0900361 if (ret) {
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700362 dev_err(ns->ctrl->dev, "L2P table transfer failed (%d)\n",
Matias Bjørlingca064082015-10-29 17:57:29 +0900363 ret);
364 ret = -EIO;
365 goto out;
366 }
367
368 if (update_l2p(cmd_slba, cmd_nlb, entries, priv)) {
369 ret = -EINTR;
370 goto out;
371 }
372
373 cmd_slba += cmd_nlb;
374 nlb -= cmd_nlb;
375 }
376
377out:
378 kfree(entries);
379 return ret;
380}
381
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100382static void nvme_nvm_bb_tbl_fold(struct nvm_dev *nvmdev,
383 int nr_dst_blks, u8 *dst_blks,
384 int nr_src_blks, u8 *src_blks)
385{
386 int blk, offset, pl, blktype;
387
388 for (blk = 0; blk < nr_dst_blks; blk++) {
389 offset = blk * nvmdev->plane_mode;
390 blktype = src_blks[offset];
391
392 /* Bad blocks on any planes take precedence over other types */
393 for (pl = 0; pl < nvmdev->plane_mode; pl++) {
394 if (src_blks[offset + pl] &
395 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
396 blktype = src_blks[offset + pl];
397 break;
398 }
399 }
400
401 dst_blks[blk] = blktype;
402 }
403}
404
Matias Bjørling08236c62015-11-28 16:49:27 +0100405static int nvme_nvm_get_bb_tbl(struct nvm_dev *nvmdev, struct ppa_addr ppa,
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100406 int nr_dst_blks, nvm_bb_update_fn *update_bbtbl,
Matias Bjørling11450462015-11-16 15:34:37 +0100407 void *priv)
Matias Bjørlingca064082015-10-29 17:57:29 +0900408{
Matias Bjørling08236c62015-11-28 16:49:27 +0100409 struct request_queue *q = nvmdev->q;
Matias Bjørlingca064082015-10-29 17:57:29 +0900410 struct nvme_ns *ns = q->queuedata;
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700411 struct nvme_ctrl *ctrl = ns->ctrl;
Matias Bjørlingca064082015-10-29 17:57:29 +0900412 struct nvme_nvm_command c = {};
Matias Bjørling11450462015-11-16 15:34:37 +0100413 struct nvme_nvm_bb_tbl *bb_tbl;
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100414 u8 *dst_blks = NULL;
415 int nr_src_blks = nr_dst_blks * nvmdev->plane_mode;
416 int tblsz = sizeof(struct nvme_nvm_bb_tbl) + nr_src_blks;
Matias Bjørlingca064082015-10-29 17:57:29 +0900417 int ret = 0;
418
419 c.get_bb.opcode = nvme_nvm_admin_get_bb_tbl;
420 c.get_bb.nsid = cpu_to_le32(ns->ns_id);
Matias Bjørling11450462015-11-16 15:34:37 +0100421 c.get_bb.spba = cpu_to_le64(ppa.ppa);
422
423 bb_tbl = kzalloc(tblsz, GFP_KERNEL);
424 if (!bb_tbl)
Matias Bjørlingca064082015-10-29 17:57:29 +0900425 return -ENOMEM;
426
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100427 dst_blks = kzalloc(nr_dst_blks, GFP_KERNEL);
428 if (!dst_blks) {
429 ret = -ENOMEM;
430 goto out;
431 }
432
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700433 ret = nvme_submit_sync_cmd(ctrl->admin_q, (struct nvme_command *)&c,
Wenwei Tao47b31152015-11-20 13:47:55 +0100434 bb_tbl, tblsz);
Matias Bjørlingca064082015-10-29 17:57:29 +0900435 if (ret) {
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700436 dev_err(ctrl->dev, "get bad block table failed (%d)\n", ret);
Matias Bjørlingca064082015-10-29 17:57:29 +0900437 ret = -EIO;
438 goto out;
439 }
440
Matias Bjørling11450462015-11-16 15:34:37 +0100441 if (bb_tbl->tblid[0] != 'B' || bb_tbl->tblid[1] != 'B' ||
442 bb_tbl->tblid[2] != 'L' || bb_tbl->tblid[3] != 'T') {
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700443 dev_err(ctrl->dev, "bbt format mismatch\n");
Matias Bjørling11450462015-11-16 15:34:37 +0100444 ret = -EINVAL;
445 goto out;
446 }
447
448 if (le16_to_cpu(bb_tbl->verid) != 1) {
449 ret = -EINVAL;
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700450 dev_err(ctrl->dev, "bbt version not supported\n");
Matias Bjørling11450462015-11-16 15:34:37 +0100451 goto out;
452 }
453
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100454 if (le32_to_cpu(bb_tbl->tblks) != nr_src_blks) {
Matias Bjørling11450462015-11-16 15:34:37 +0100455 ret = -EINVAL;
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700456 dev_err(ctrl->dev, "bbt unsuspected blocks returned (%u!=%u)",
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100457 le32_to_cpu(bb_tbl->tblks), nr_src_blks);
Matias Bjørling11450462015-11-16 15:34:37 +0100458 goto out;
459 }
460
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100461 nvme_nvm_bb_tbl_fold(nvmdev, nr_dst_blks, dst_blks,
462 nr_src_blks, bb_tbl->blk);
463
Matias Bjørling08236c62015-11-28 16:49:27 +0100464 ppa = dev_to_generic_addr(nvmdev, ppa);
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100465 ret = update_bbtbl(ppa, nr_dst_blks, dst_blks, priv);
466
Matias Bjørlingca064082015-10-29 17:57:29 +0900467out:
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100468 kfree(dst_blks);
Matias Bjørling11450462015-11-16 15:34:37 +0100469 kfree(bb_tbl);
470 return ret;
471}
472
Matias Bjørling16f26c32015-12-06 11:25:48 +0100473static int nvme_nvm_set_bb_tbl(struct nvm_dev *nvmdev, struct nvm_rq *rqd,
Matias Bjørling11450462015-11-16 15:34:37 +0100474 int type)
475{
Matias Bjørling16f26c32015-12-06 11:25:48 +0100476 struct nvme_ns *ns = nvmdev->q->queuedata;
Matias Bjørling11450462015-11-16 15:34:37 +0100477 struct nvme_nvm_command c = {};
478 int ret = 0;
479
480 c.set_bb.opcode = nvme_nvm_admin_set_bb_tbl;
481 c.set_bb.nsid = cpu_to_le32(ns->ns_id);
482 c.set_bb.spba = cpu_to_le64(rqd->ppa_addr.ppa);
483 c.set_bb.nlb = cpu_to_le16(rqd->nr_pages - 1);
484 c.set_bb.value = type;
485
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700486 ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, (struct nvme_command *)&c,
Wenwei Tao47b31152015-11-20 13:47:55 +0100487 NULL, 0);
Matias Bjørling11450462015-11-16 15:34:37 +0100488 if (ret)
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700489 dev_err(ns->ctrl->dev, "set bad block table failed (%d)\n", ret);
Matias Bjørlingca064082015-10-29 17:57:29 +0900490 return ret;
491}
492
493static inline void nvme_nvm_rqtocmd(struct request *rq, struct nvm_rq *rqd,
494 struct nvme_ns *ns, struct nvme_nvm_command *c)
495{
496 c->ph_rw.opcode = rqd->opcode;
497 c->ph_rw.nsid = cpu_to_le32(ns->ns_id);
498 c->ph_rw.spba = cpu_to_le64(rqd->ppa_addr.ppa);
499 c->ph_rw.control = cpu_to_le16(rqd->flags);
500 c->ph_rw.length = cpu_to_le16(rqd->nr_pages - 1);
501
502 if (rqd->opcode == NVM_OP_HBWRITE || rqd->opcode == NVM_OP_HBREAD)
503 c->hb_rw.slba = cpu_to_le64(nvme_block_nr(ns,
504 rqd->bio->bi_iter.bi_sector));
505}
506
507static void nvme_nvm_end_io(struct request *rq, int error)
508{
509 struct nvm_rq *rqd = rq->end_io_data;
Matias Bjørlingca064082015-10-29 17:57:29 +0900510
Matias Bjørling912761622016-01-12 07:49:21 +0100511 nvm_end_io(rqd, error);
Matias Bjørlingca064082015-10-29 17:57:29 +0900512
513 kfree(rq->cmd);
514 blk_mq_free_request(rq);
515}
516
Matias Bjørling16f26c32015-12-06 11:25:48 +0100517static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
Matias Bjørlingca064082015-10-29 17:57:29 +0900518{
Matias Bjørling16f26c32015-12-06 11:25:48 +0100519 struct request_queue *q = dev->q;
Matias Bjørlingca064082015-10-29 17:57:29 +0900520 struct nvme_ns *ns = q->queuedata;
521 struct request *rq;
522 struct bio *bio = rqd->bio;
523 struct nvme_nvm_command *cmd;
524
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100525 rq = blk_mq_alloc_request(q, bio_rw(bio), 0);
Matias Bjørlingca064082015-10-29 17:57:29 +0900526 if (IS_ERR(rq))
527 return -ENOMEM;
528
529 cmd = kzalloc(sizeof(struct nvme_nvm_command), GFP_KERNEL);
530 if (!cmd) {
531 blk_mq_free_request(rq);
532 return -ENOMEM;
533 }
534
535 rq->cmd_type = REQ_TYPE_DRV_PRIV;
536 rq->ioprio = bio_prio(bio);
537
538 if (bio_has_data(bio))
539 rq->nr_phys_segments = bio_phys_segments(q, bio);
540
541 rq->__data_len = bio->bi_iter.bi_size;
542 rq->bio = rq->biotail = bio;
543
544 nvme_nvm_rqtocmd(rq, rqd, ns, cmd);
545
546 rq->cmd = (unsigned char *)cmd;
547 rq->cmd_len = sizeof(struct nvme_nvm_command);
548 rq->special = (void *)0;
549
550 rq->end_io_data = rqd;
551
552 blk_execute_rq_nowait(q, NULL, rq, 0, nvme_nvm_end_io);
553
554 return 0;
555}
556
Matias Bjørling16f26c32015-12-06 11:25:48 +0100557static int nvme_nvm_erase_block(struct nvm_dev *dev, struct nvm_rq *rqd)
Matias Bjørlingca064082015-10-29 17:57:29 +0900558{
Matias Bjørling16f26c32015-12-06 11:25:48 +0100559 struct request_queue *q = dev->q;
Matias Bjørlingca064082015-10-29 17:57:29 +0900560 struct nvme_ns *ns = q->queuedata;
561 struct nvme_nvm_command c = {};
562
563 c.erase.opcode = NVM_OP_ERASE;
564 c.erase.nsid = cpu_to_le32(ns->ns_id);
565 c.erase.spba = cpu_to_le64(rqd->ppa_addr.ppa);
566 c.erase.length = cpu_to_le16(rqd->nr_pages - 1);
567
568 return nvme_submit_sync_cmd(q, (struct nvme_command *)&c, NULL, 0);
569}
570
Matias Bjørling16f26c32015-12-06 11:25:48 +0100571static void *nvme_nvm_create_dma_pool(struct nvm_dev *nvmdev, char *name)
Matias Bjørlingca064082015-10-29 17:57:29 +0900572{
Matias Bjørling16f26c32015-12-06 11:25:48 +0100573 struct nvme_ns *ns = nvmdev->q->queuedata;
Matias Bjørlingca064082015-10-29 17:57:29 +0900574
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700575 return dma_pool_create(name, ns->ctrl->dev, PAGE_SIZE, PAGE_SIZE, 0);
Matias Bjørlingca064082015-10-29 17:57:29 +0900576}
577
578static void nvme_nvm_destroy_dma_pool(void *pool)
579{
580 struct dma_pool *dma_pool = pool;
581
582 dma_pool_destroy(dma_pool);
583}
584
Matias Bjørling16f26c32015-12-06 11:25:48 +0100585static void *nvme_nvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
Matias Bjørlingca064082015-10-29 17:57:29 +0900586 gfp_t mem_flags, dma_addr_t *dma_handler)
587{
588 return dma_pool_alloc(pool, mem_flags, dma_handler);
589}
590
591static void nvme_nvm_dev_dma_free(void *pool, void *ppa_list,
592 dma_addr_t dma_handler)
593{
594 dma_pool_free(pool, ppa_list, dma_handler);
595}
596
597static struct nvm_dev_ops nvme_nvm_dev_ops = {
598 .identity = nvme_nvm_identity,
599
600 .get_l2p_tbl = nvme_nvm_get_l2p_tbl,
601
602 .get_bb_tbl = nvme_nvm_get_bb_tbl,
Matias Bjørling11450462015-11-16 15:34:37 +0100603 .set_bb_tbl = nvme_nvm_set_bb_tbl,
Matias Bjørlingca064082015-10-29 17:57:29 +0900604
605 .submit_io = nvme_nvm_submit_io,
606 .erase_block = nvme_nvm_erase_block,
607
608 .create_dma_pool = nvme_nvm_create_dma_pool,
609 .destroy_dma_pool = nvme_nvm_destroy_dma_pool,
610 .dev_dma_alloc = nvme_nvm_dev_dma_alloc,
611 .dev_dma_free = nvme_nvm_dev_dma_free,
612
613 .max_phys_sect = 64,
614};
615
616int nvme_nvm_register(struct request_queue *q, char *disk_name)
617{
618 return nvm_register(q, disk_name, &nvme_nvm_dev_ops);
619}
620
621void nvme_nvm_unregister(struct request_queue *q, char *disk_name)
622{
623 nvm_unregister(disk_name);
624}
625
Matias Bjørling09f2e712015-11-28 16:49:26 +0100626/* move to shared place when used in multiple places. */
627#define PCI_VENDOR_ID_CNEX 0x1d1d
628#define PCI_DEVICE_ID_CNEX_WL 0x2807
629#define PCI_DEVICE_ID_CNEX_QEMU 0x1f1f
630
Matias Bjørlingca064082015-10-29 17:57:29 +0900631int nvme_nvm_ns_supported(struct nvme_ns *ns, struct nvme_id_ns *id)
632{
Christoph Hellwigac02ddd2015-12-03 09:52:05 -0700633 struct nvme_ctrl *ctrl = ns->ctrl;
634 /* XXX: this is poking into PCI structures from generic code! */
635 struct pci_dev *pdev = to_pci_dev(ctrl->dev);
Matias Bjørlingca064082015-10-29 17:57:29 +0900636
637 /* QEMU NVMe simulator - PCI ID + Vendor specific bit */
Matias Bjørling09f2e712015-11-28 16:49:26 +0100638 if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
639 pdev->device == PCI_DEVICE_ID_CNEX_QEMU &&
Matias Bjørlingca064082015-10-29 17:57:29 +0900640 id->vs[0] == 0x1)
641 return 1;
642
643 /* CNEX Labs - PCI ID + Vendor specific bit */
Matias Bjørling09f2e712015-11-28 16:49:26 +0100644 if (pdev->vendor == PCI_VENDOR_ID_CNEX &&
645 pdev->device == PCI_DEVICE_ID_CNEX_WL &&
Matias Bjørlingca064082015-10-29 17:57:29 +0900646 id->vs[0] == 0x1)
647 return 1;
648
649 return 0;
650}