2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
37 #include <sys/module.h>
40 #include <dev/pci/pcivar.h>
42 #include "nvme_private.h"
45 nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
48 struct nvme_namespace *ns;
49 struct nvme_controller *ctrlr;
50 struct nvme_pt_command *pt;
58 nvme_ns_test(ns, cmd, arg);
60 case NVME_PASSTHROUGH_CMD:
61 pt = (struct nvme_pt_command *)arg;
62 return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, ns->id,
63 1 /* is_user_buffer */, 0 /* is_admin_cmd */));
65 *(off_t *)arg = (off_t)nvme_ns_get_size(ns);
68 *(u_int *)arg = nvme_ns_get_sector_size(ns);
78 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused,
84 error = securelevel_gt(td->td_ucred, 0);
90 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
98 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
100 struct bio *bp = arg;
103 * TODO: add more extensive translation of NVMe status codes
104 * to different bio error codes (i.e. EIO, EINVAL, etc.)
106 if (nvme_completion_is_error(cpl)) {
108 bp->bio_flags |= BIO_ERROR;
109 bp->bio_resid = bp->bio_bcount;
117 nvme_ns_strategy(struct bio *bp)
119 struct nvme_namespace *ns;
122 ns = bp->bio_dev->si_drv1;
123 err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);
127 bp->bio_flags |= BIO_ERROR;
128 bp->bio_resid = bp->bio_bcount;
134 static struct cdevsw nvme_ns_cdevsw = {
135 .d_version = D_VERSION,
136 #ifdef NVME_UNMAPPED_BIO_SUPPORT
137 .d_flags = D_DISK | D_UNMAPPED_IO,
142 .d_write = physwrite,
143 .d_open = nvme_ns_open,
144 .d_close = nvme_ns_close,
145 .d_strategy = nvme_ns_strategy,
146 .d_ioctl = nvme_ns_ioctl
150 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
152 return ns->ctrlr->max_xfer_size;
156 nvme_ns_get_sector_size(struct nvme_namespace *ns)
158 return (1 << ns->data.lbaf[ns->data.flbas.format].lbads);
162 nvme_ns_get_num_sectors(struct nvme_namespace *ns)
164 return (ns->data.nsze);
168 nvme_ns_get_size(struct nvme_namespace *ns)
170 return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
174 nvme_ns_get_flags(struct nvme_namespace *ns)
180 nvme_ns_get_serial_number(struct nvme_namespace *ns)
182 return ((const char *)ns->ctrlr->cdata.sn);
186 nvme_ns_get_model_number(struct nvme_namespace *ns)
188 return ((const char *)ns->ctrlr->cdata.mn);
191 const struct nvme_namespace_data *
192 nvme_ns_get_data(struct nvme_namespace *ns)
199 nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
201 struct bio *bp = arg;
202 nvme_cb_fn_t bp_cb_fn;
204 bp_cb_fn = bp->bio_driver1;
207 free(bp->bio_driver2, M_NVME);
209 bp_cb_fn(bp, status);
213 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
216 struct nvme_dsm_range *dsm_range;
219 bp->bio_driver1 = cb_fn;
221 switch (bp->bio_cmd) {
223 err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
226 err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
229 err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
233 malloc(sizeof(struct nvme_dsm_range), M_NVME,
236 bp->bio_bcount/nvme_ns_get_sector_size(ns);
237 dsm_range->starting_lba =
238 bp->bio_offset/nvme_ns_get_sector_size(ns);
239 bp->bio_driver2 = dsm_range;
240 err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
241 nvme_ns_bio_done, bp);
243 free(dsm_range, M_NVME);
255 nvme_ns_populate_chatham_data(struct nvme_namespace *ns)
257 struct nvme_controller *ctrlr;
258 struct nvme_namespace_data *nsdata;
263 nsdata->nsze = ctrlr->chatham_lbas;
264 nsdata->ncap = ctrlr->chatham_lbas;
265 nsdata->nuse = ctrlr->chatham_lbas;
267 /* Chatham2 doesn't support thin provisioning. */
268 nsdata->nsfeat.thin_prov = 0;
270 /* Set LBA size to 512 bytes. */
271 nsdata->lbaf[0].lbads = 9;
273 #endif /* CHATHAM2 */
276 nvme_ns_construct(struct nvme_namespace *ns, uint16_t id,
277 struct nvme_controller *ctrlr)
279 struct nvme_completion_poll_status status;
285 * Namespaces are reconstructed after a controller reset, so check
286 * to make sure we only call mtx_init once on each mtx.
288 * TODO: Move this somewhere where it gets called at controller
289 * construction time, which is not invoked as part of each
292 if (!mtx_initialized(&ns->lock))
293 mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
296 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID)
297 nvme_ns_populate_chatham_data(ns);
301 nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
302 nvme_completion_poll_cb, &status);
303 while (status.done == FALSE)
305 if (nvme_completion_is_error(&status.cpl)) {
306 nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
314 * Note: format is a 0-based value, so > is appropriate here,
317 if (ns->data.flbas.format > ns->data.nlbaf) {
318 printf("lba format %d exceeds number supported (%d)\n",
319 ns->data.flbas.format, ns->data.nlbaf+1);
323 if (ctrlr->cdata.oncs.dsm)
324 ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
326 if (ctrlr->cdata.vwc.present)
327 ns->flags |= NVME_NS_FLUSH_SUPPORTED;
330 * cdev may have already been created, if we are reconstructing the
331 * namespace after a controller-level reset.
333 if (ns->cdev != NULL)
337 * MAKEDEV_ETERNAL was added in r210923, for cdevs that will never
338 * be destroyed. This avoids refcounting on the cdev object.
339 * That should be OK case here, as long as we're not supporting PCIe
340 * surprise removal nor namespace deletion.
342 #ifdef MAKEDEV_ETERNAL_KLD
343 ns->cdev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &nvme_ns_cdevsw, 0,
344 NULL, UID_ROOT, GID_WHEEL, 0600, "nvme%dns%d",
345 device_get_unit(ctrlr->dev), ns->id);
347 ns->cdev = make_dev_credf(0, &nvme_ns_cdevsw, 0,
348 NULL, UID_ROOT, GID_WHEEL, 0600, "nvme%dns%d",
349 device_get_unit(ctrlr->dev), ns->id);
352 if (ns->cdev != NULL)
353 ns->cdev->si_drv1 = ns;
358 void nvme_ns_destruct(struct nvme_namespace *ns)
361 if (ns->cdev != NULL)
362 destroy_dev(ns->cdev);