1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2012-2013 Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/disk.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/proc.h>
40 #include <sys/systm.h>
41
42 #include <dev/pci/pcivar.h>
43
44 #include <geom/geom.h>
45
46 #include "nvme_private.h"
47
48 static void nvme_bio_child_inbed(struct bio *parent, int bio_error);
49 static void nvme_bio_child_done(void *arg,
50 const struct nvme_completion *cpl);
51 static uint32_t nvme_get_num_segments(uint64_t addr, uint64_t size,
52 uint32_t alignment);
53 static void nvme_free_child_bios(int num_bios,
54 struct bio **child_bios);
55 static struct bio ** nvme_allocate_child_bios(int num_bios);
56 static struct bio ** nvme_construct_child_bios(struct bio *bp,
57 uint32_t alignment,
58 int *num_bios);
59 static int nvme_ns_split_bio(struct nvme_namespace *ns,
60 struct bio *bp,
61 uint32_t alignment);
62
63 static int
nvme_ns_ioctl(struct cdev * cdev,u_long cmd,caddr_t arg,int flag,struct thread * td)64 nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
65 struct thread *td)
66 {
67 struct nvme_namespace *ns;
68 struct nvme_controller *ctrlr;
69 struct nvme_pt_command *pt;
70
71 ns = cdev->si_drv1;
72 ctrlr = ns->ctrlr;
73
74 switch (cmd) {
75 case NVME_IO_TEST:
76 case NVME_BIO_TEST:
77 nvme_ns_test(ns, cmd, arg);
78 break;
79 case NVME_PASSTHROUGH_CMD:
80 pt = (struct nvme_pt_command *)arg;
81 return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, ns->id,
82 1 /* is_user_buffer */, 0 /* is_admin_cmd */));
83 case NVME_GET_NSID:
84 {
85 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg;
86 strncpy(gnsid->cdev, device_get_nameunit(ctrlr->dev),
87 sizeof(gnsid->cdev));
88 gnsid->cdev[sizeof(gnsid->cdev) - 1] = '\0';
89 gnsid->nsid = ns->id;
90 break;
91 }
92 case DIOCGMEDIASIZE:
93 *(off_t *)arg = (off_t)nvme_ns_get_size(ns);
94 break;
95 case DIOCGSECTORSIZE:
96 *(u_int *)arg = nvme_ns_get_sector_size(ns);
97 break;
98 default:
99 return (ENOTTY);
100 }
101
102 return (0);
103 }
104
105 static int
nvme_ns_open(struct cdev * dev __unused,int flags,int fmt __unused,struct thread * td)106 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused,
107 struct thread *td)
108 {
109 int error = 0;
110
111 if (flags & FWRITE)
112 error = securelevel_gt(td->td_ucred, 0);
113
114 return (error);
115 }
116
117 static int
nvme_ns_close(struct cdev * dev __unused,int flags,int fmt __unused,struct thread * td)118 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
119 struct thread *td)
120 {
121
122 return (0);
123 }
124
125 static void
nvme_ns_strategy_done(void * arg,const struct nvme_completion * cpl)126 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
127 {
128 struct bio *bp = arg;
129
130 /*
131 * TODO: add more extensive translation of NVMe status codes
132 * to different bio error codes (i.e. EIO, EINVAL, etc.)
133 */
134 if (nvme_completion_is_error(cpl)) {
135 bp->bio_error = EIO;
136 bp->bio_flags |= BIO_ERROR;
137 bp->bio_resid = bp->bio_bcount;
138 } else
139 bp->bio_resid = 0;
140
141 biodone(bp);
142 }
143
144 static void
nvme_ns_strategy(struct bio * bp)145 nvme_ns_strategy(struct bio *bp)
146 {
147 struct nvme_namespace *ns;
148 int err;
149
150 ns = bp->bio_dev->si_drv1;
151 err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);
152
153 if (err) {
154 bp->bio_error = err;
155 bp->bio_flags |= BIO_ERROR;
156 bp->bio_resid = bp->bio_bcount;
157 biodone(bp);
158 }
159
160 }
161
162 static struct cdevsw nvme_ns_cdevsw = {
163 .d_version = D_VERSION,
164 .d_flags = D_DISK,
165 .d_read = physread,
166 .d_write = physwrite,
167 .d_open = nvme_ns_open,
168 .d_close = nvme_ns_close,
169 .d_strategy = nvme_ns_strategy,
170 .d_ioctl = nvme_ns_ioctl
171 };
172
173 uint32_t
nvme_ns_get_max_io_xfer_size(struct nvme_namespace * ns)174 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
175 {
176 return ns->ctrlr->max_xfer_size;
177 }
178
179 uint32_t
nvme_ns_get_sector_size(struct nvme_namespace * ns)180 nvme_ns_get_sector_size(struct nvme_namespace *ns)
181 {
182 uint8_t flbas_fmt, lbads;
183
184 flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
185 lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, ns->data.lbaf[flbas_fmt]);
186
187 return (1 << lbads);
188 }
189
190 uint64_t
nvme_ns_get_num_sectors(struct nvme_namespace * ns)191 nvme_ns_get_num_sectors(struct nvme_namespace *ns)
192 {
193 return (ns->data.nsze);
194 }
195
196 uint64_t
nvme_ns_get_size(struct nvme_namespace * ns)197 nvme_ns_get_size(struct nvme_namespace *ns)
198 {
199 return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
200 }
201
202 uint32_t
nvme_ns_get_flags(struct nvme_namespace * ns)203 nvme_ns_get_flags(struct nvme_namespace *ns)
204 {
205 return (ns->flags);
206 }
207
208 const char *
nvme_ns_get_serial_number(struct nvme_namespace * ns)209 nvme_ns_get_serial_number(struct nvme_namespace *ns)
210 {
211 return ((const char *)ns->ctrlr->cdata.sn);
212 }
213
214 const char *
nvme_ns_get_model_number(struct nvme_namespace * ns)215 nvme_ns_get_model_number(struct nvme_namespace *ns)
216 {
217 return ((const char *)ns->ctrlr->cdata.mn);
218 }
219
220 const struct nvme_namespace_data *
nvme_ns_get_data(struct nvme_namespace * ns)221 nvme_ns_get_data(struct nvme_namespace *ns)
222 {
223
224 return (&ns->data);
225 }
226
227 uint32_t
nvme_ns_get_stripesize(struct nvme_namespace * ns)228 nvme_ns_get_stripesize(struct nvme_namespace *ns)
229 {
230 uint32_t ss;
231
232 if (NVMEV(NVME_NS_DATA_NSFEAT_NPVALID, ns->data.nsfeat) != 0) {
233 ss = nvme_ns_get_sector_size(ns);
234 if (ns->data.npwa != 0)
235 return ((ns->data.npwa + 1) * ss);
236 else if (ns->data.npwg != 0)
237 return ((ns->data.npwg + 1) * ss);
238 }
239 return (ns->boundary);
240 }
241
242 static void
nvme_ns_bio_done(void * arg,const struct nvme_completion * status)243 nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
244 {
245 struct bio *bp = arg;
246 nvme_cb_fn_t bp_cb_fn;
247
248 bp_cb_fn = bp->bio_driver1;
249
250 if (bp->bio_driver2)
251 free(bp->bio_driver2, M_NVME);
252
253 if (nvme_completion_is_error(status)) {
254 bp->bio_flags |= BIO_ERROR;
255 if (bp->bio_error == 0)
256 bp->bio_error = EIO;
257 }
258
259 if ((bp->bio_flags & BIO_ERROR) == 0)
260 bp->bio_resid = 0;
261 else
262 bp->bio_resid = bp->bio_bcount;
263
264 bp_cb_fn(bp, status);
265 }
266
267 static void
nvme_bio_child_inbed(struct bio * parent,int bio_error)268 nvme_bio_child_inbed(struct bio *parent, int bio_error)
269 {
270 struct nvme_completion parent_cpl;
271 int children, inbed;
272
273 if (bio_error != 0) {
274 parent->bio_flags |= BIO_ERROR;
275 parent->bio_error = bio_error;
276 }
277
278 /*
279 * atomic_fetchadd will return value before adding 1, so we still
280 * must add 1 to get the updated inbed number. Save bio_children
281 * before incrementing to guard against race conditions when
282 * two children bios complete on different queues.
283 */
284 children = atomic_load_acq_int(&parent->bio_children);
285 inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1;
286 if (inbed == children) {
287 bzero(&parent_cpl, sizeof(parent_cpl));
288 if (parent->bio_flags & BIO_ERROR) {
289 parent_cpl.status &= ~NVMEM(NVME_STATUS_SC);
290 parent_cpl.status |= NVMEF(NVME_STATUS_SC,
291 NVME_SC_DATA_TRANSFER_ERROR);
292 }
293 nvme_ns_bio_done(parent, &parent_cpl);
294 }
295 }
296
297 static void
nvme_bio_child_done(void * arg,const struct nvme_completion * cpl)298 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
299 {
300 struct bio *child = arg;
301 struct bio *parent;
302 int bio_error;
303
304 parent = child->bio_parent;
305 g_destroy_bio(child);
306 bio_error = nvme_completion_is_error(cpl) ? EIO : 0;
307 nvme_bio_child_inbed(parent, bio_error);
308 }
309
310 static uint32_t
nvme_get_num_segments(uint64_t addr,uint64_t size,uint32_t align)311 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
312 {
313 uint32_t num_segs, offset, remainder;
314
315 if (align == 0)
316 return (1);
317
318 KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));
319
320 num_segs = size / align;
321 remainder = size & (align - 1);
322 offset = addr & (align - 1);
323 if (remainder > 0 || offset > 0)
324 num_segs += 1 + (remainder + offset - 1) / align;
325 return (num_segs);
326 }
327
328 static void
nvme_free_child_bios(int num_bios,struct bio ** child_bios)329 nvme_free_child_bios(int num_bios, struct bio **child_bios)
330 {
331 int i;
332
333 for (i = 0; i < num_bios; i++) {
334 if (child_bios[i] != NULL)
335 g_destroy_bio(child_bios[i]);
336 }
337
338 free(child_bios, M_NVME);
339 }
340
341 static struct bio **
nvme_allocate_child_bios(int num_bios)342 nvme_allocate_child_bios(int num_bios)
343 {
344 struct bio **child_bios;
345 int err = 0, i;
346
347 child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
348 if (child_bios == NULL)
349 return (NULL);
350
351 for (i = 0; i < num_bios; i++) {
352 child_bios[i] = g_new_bio();
353 if (child_bios[i] == NULL)
354 err = ENOMEM;
355 }
356
357 if (err == ENOMEM) {
358 nvme_free_child_bios(num_bios, child_bios);
359 return (NULL);
360 }
361
362 return (child_bios);
363 }
364
365 static struct bio **
nvme_construct_child_bios(struct bio * bp,uint32_t alignment,int * num_bios)366 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
367 {
368 struct bio **child_bios;
369 struct bio *child;
370 uint64_t cur_offset;
371 caddr_t data;
372 uint32_t rem_bcount;
373 int i;
374 struct vm_page **ma;
375 uint32_t ma_offset;
376
377 *num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
378 alignment);
379 child_bios = nvme_allocate_child_bios(*num_bios);
380 if (child_bios == NULL)
381 return (NULL);
382
383 bp->bio_children = *num_bios;
384 bp->bio_inbed = 0;
385 cur_offset = bp->bio_offset;
386 rem_bcount = bp->bio_bcount;
387 data = bp->bio_data;
388 ma_offset = bp->bio_ma_offset;
389 ma = bp->bio_ma;
390
391 for (i = 0; i < *num_bios; i++) {
392 child = child_bios[i];
393 child->bio_parent = bp;
394 child->bio_cmd = bp->bio_cmd;
395 child->bio_offset = cur_offset;
396 child->bio_bcount = min(rem_bcount,
397 alignment - (cur_offset & (alignment - 1)));
398 child->bio_flags = bp->bio_flags;
399 if (bp->bio_flags & BIO_UNMAPPED) {
400 child->bio_ma_offset = ma_offset;
401 child->bio_ma = ma;
402 child->bio_ma_n =
403 nvme_get_num_segments(child->bio_ma_offset,
404 child->bio_bcount, PAGE_SIZE);
405 ma_offset = (ma_offset + child->bio_bcount) &
406 PAGE_MASK;
407 ma += child->bio_ma_n;
408 if (ma_offset != 0)
409 ma -= 1;
410 } else {
411 child->bio_data = data;
412 data += child->bio_bcount;
413 }
414 cur_offset += child->bio_bcount;
415 rem_bcount -= child->bio_bcount;
416 }
417
418 return (child_bios);
419 }
420
421 static int
nvme_ns_split_bio(struct nvme_namespace * ns,struct bio * bp,uint32_t alignment)422 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
423 uint32_t alignment)
424 {
425 struct bio *child;
426 struct bio **child_bios;
427 int err, i, num_bios;
428
429 child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
430 if (child_bios == NULL)
431 return (ENOMEM);
432
433 for (i = 0; i < num_bios; i++) {
434 child = child_bios[i];
435 err = nvme_ns_bio_process(ns, child, nvme_bio_child_done);
436 if (err != 0) {
437 nvme_bio_child_inbed(bp, err);
438 g_destroy_bio(child);
439 }
440 }
441
442 free(child_bios, M_NVME);
443 return (0);
444 }
445
446 int
nvme_ns_bio_process(struct nvme_namespace * ns,struct bio * bp,nvme_cb_fn_t cb_fn)447 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
448 nvme_cb_fn_t cb_fn)
449 {
450 struct nvme_dsm_range *dsm_range;
451 uint32_t num_bios;
452 int err;
453
454 bp->bio_driver1 = cb_fn;
455
456 if (ns->boundary > 0 &&
457 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
458 num_bios = nvme_get_num_segments(bp->bio_offset,
459 bp->bio_bcount, ns->boundary);
460 if (num_bios > 1)
461 return (nvme_ns_split_bio(ns, bp, ns->boundary));
462 }
463
464 switch (bp->bio_cmd) {
465 case BIO_READ:
466 err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
467 break;
468 case BIO_WRITE:
469 err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
470 break;
471 case BIO_FLUSH:
472 err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
473 break;
474 case BIO_DELETE:
475 dsm_range =
476 malloc(sizeof(struct nvme_dsm_range), M_NVME,
477 M_ZERO | M_NOWAIT);
478 if (!dsm_range) {
479 err = ENOMEM;
480 break;
481 }
482 dsm_range->length =
483 htole32(bp->bio_bcount/nvme_ns_get_sector_size(ns));
484 dsm_range->starting_lba =
485 htole64(bp->bio_offset/nvme_ns_get_sector_size(ns));
486 bp->bio_driver2 = dsm_range;
487 err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
488 nvme_ns_bio_done, bp);
489 if (err != 0)
490 free(dsm_range, M_NVME);
491 break;
492 default:
493 err = EOPNOTSUPP;
494 break;
495 }
496
497 return (err);
498 }
499
500 int
nvme_ns_ioctl_process(struct nvme_namespace * ns,u_long cmd,caddr_t arg,int flag,struct thread * td)501 nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, caddr_t arg,
502 int flag, struct thread *td)
503 {
504 return (nvme_ns_ioctl(ns->cdev, cmd, arg, flag, td));
505 }
506
507 int
nvme_ns_construct(struct nvme_namespace * ns,uint32_t id,struct nvme_controller * ctrlr)508 nvme_ns_construct(struct nvme_namespace *ns, uint32_t id,
509 struct nvme_controller *ctrlr)
510 {
511 struct make_dev_args md_args;
512 struct nvme_completion_poll_status status;
513 int res;
514 int unit;
515 uint8_t flbas_fmt;
516 uint8_t vwc_present;
517
518 ns->ctrlr = ctrlr;
519 ns->id = id;
520
521 /*
522 * Namespaces are reconstructed after a controller reset, so check
523 * to make sure we only call mtx_init once on each mtx.
524 *
525 * TODO: Move this somewhere where it gets called at controller
526 * construction time, which is not invoked as part of each
527 * controller reset.
528 */
529 if (!mtx_initialized(&ns->lock))
530 mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
531
532 status.done = 0;
533 nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
534 nvme_completion_poll_cb, &status);
535 nvme_completion_poll(&status);
536 if (nvme_completion_is_error(&status.cpl)) {
537 nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
538 return (ENXIO);
539 }
540
541 /* Convert data to host endian */
542 nvme_namespace_data_swapbytes(&ns->data);
543
544 /*
545 * If the size of is zero, chances are this isn't a valid
546 * namespace (eg one that's not been configured yet). The
547 * standard says the entire id will be zeros, so this is a
548 * cheap way to test for that.
549 */
550 if (ns->data.nsze == 0)
551 return (ENXIO);
552
553 flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
554
555 /*
556 * Note: format is a 0-based value, so > is appropriate here,
557 * not >=.
558 */
559 if (flbas_fmt > ns->data.nlbaf) {
560 nvme_printf(ctrlr,
561 "lba format %d exceeds number supported (%d)\n",
562 flbas_fmt, ns->data.nlbaf + 1);
563 return (ENXIO);
564 }
565
566 /*
567 * Older Intel devices (like the PC35xxx and P45xx series) advertise in
568 * vendor specific space an alignment that improves performance. If
569 * present use for the stripe size. NVMe 1.3 standardized this as
570 * NOIOB, and newer Intel drives use that.
571 */
572 if ((ctrlr->quirks & QUIRK_INTEL_ALIGNMENT) != 0) {
573 if (ctrlr->cdata.vs[3] != 0)
574 ns->boundary =
575 1 << (ctrlr->cdata.vs[3] + NVME_MPS_SHIFT +
576 NVME_CAP_HI_MPSMIN(ctrlr->cap_hi));
577 else
578 ns->boundary = 0;
579 } else {
580 ns->boundary = ns->data.noiob * nvme_ns_get_sector_size(ns);
581 }
582
583 if (nvme_ctrlr_has_dataset_mgmt(&ctrlr->cdata))
584 ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
585
586 vwc_present = NVMEV(NVME_CTRLR_DATA_VWC_PRESENT, ctrlr->cdata.vwc);
587 if (vwc_present)
588 ns->flags |= NVME_NS_FLUSH_SUPPORTED;
589
590 /*
591 * cdev may have already been created, if we are reconstructing the
592 * namespace after a controller-level reset.
593 */
594 if (ns->cdev != NULL)
595 return (0);
596
597 /*
598 * Namespace IDs start at 1, so we need to subtract 1 to create a
599 * correct unit number.
600 */
601 unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;
602
603 make_dev_args_init(&md_args);
604 md_args.mda_devsw = &nvme_ns_cdevsw;
605 md_args.mda_unit = unit;
606 md_args.mda_mode = 0600;
607 md_args.mda_si_drv1 = ns;
608 res = make_dev_s(&md_args, &ns->cdev, "nvme%dns%d",
609 device_get_unit(ctrlr->dev), ns->id);
610 if (res != 0)
611 return (ENXIO);
612
613 ns->cdev->si_flags |= SI_UNMAPPED;
614
615 return (0);
616 }
617
618 void
nvme_ns_destruct(struct nvme_namespace * ns)619 nvme_ns_destruct(struct nvme_namespace *ns)
620 {
621
622 if (ns->cdev != NULL)
623 destroy_dev(ns->cdev);
624 }
625