2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Netflix, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
42 #include <sys/fcntl.h>
43 #include <sys/interrupt.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_internal.h>
59 #include <cam/cam_debug.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/nvme/nvme_all.h>
64 #include <machine/stdarg.h> /* for xpt_print below */
67 struct nvme_quirk_entry {
69 #define CAM_QUIRK_MAXTAGS 1
74 /* Not even sure why we need this */
75 static periph_init_t nvme_probe_periph_init;
77 static struct periph_driver nvme_probe_driver =
79 nvme_probe_periph_init, "nvme_probe",
80 TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
84 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
93 static char *nvme_probe_action_text[] = {
94 "NVME_PROBE_IDENTIFY",
100 #define NVME_PROBE_SET_ACTION(softc, newaction) \
103 text = nvme_probe_action_text; \
104 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
105 ("Probe %s to %s\n", text[(softc)->action], \
106 text[(newaction)])); \
107 (softc)->action = (newaction); \
111 NVME_PROBE_NO_ANNOUNCE = 0x04
115 TAILQ_HEAD(, ccb_hdr) request_ccbs;
116 nvme_probe_action action;
117 nvme_probe_flags flags;
119 struct cam_periph *periph;
122 static struct nvme_quirk_entry nvme_quirk_table[] =
126 // T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
127 // /*vendor*/"*", /*product*/"*", /*revision*/"*"
129 .quirks = 0, .mintags = 0, .maxtags = 0
133 static const int nvme_quirk_table_size =
134 sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
136 static cam_status nvme_probe_register(struct cam_periph *periph,
138 static void nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
139 static void nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
140 static void nvme_probe_cleanup(struct cam_periph *periph);
141 //static void nvme_find_quirk(struct cam_ed *device);
142 static void nvme_scan_lun(struct cam_periph *periph,
143 struct cam_path *path, cam_flags flags,
145 static struct cam_ed *
146 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
148 static void nvme_device_transport(struct cam_path *path);
149 static void nvme_dev_async(u_int32_t async_code,
151 struct cam_et *target,
152 struct cam_ed *device,
154 static void nvme_action(union ccb *start_ccb);
155 static void nvme_announce_periph(struct cam_periph *periph);
156 static void nvme_proto_announce(struct cam_ed *device);
157 static void nvme_proto_denounce(struct cam_ed *device);
158 static void nvme_proto_debug_out(union ccb *ccb);
160 static struct xpt_xport_ops nvme_xport_ops = {
161 .alloc_device = nvme_alloc_device,
162 .action = nvme_action,
163 .async = nvme_dev_async,
164 .announce = nvme_announce_periph,
166 #define NVME_XPT_XPORT(x, X) \
167 static struct xpt_xport nvme_xport_ ## x = { \
168 .xport = XPORT_ ## X, \
170 .ops = &nvme_xport_ops, \
172 CAM_XPT_XPORT(nvme_xport_ ## x);
174 NVME_XPT_XPORT(nvme, NVME);
176 #undef NVME_XPT_XPORT
178 static struct xpt_proto_ops nvme_proto_ops = {
179 .announce = nvme_proto_announce,
180 .denounce = nvme_proto_denounce,
181 .debug_out = nvme_proto_debug_out,
183 static struct xpt_proto nvme_proto = {
186 .ops = &nvme_proto_ops,
188 CAM_XPT_PROTO(nvme_proto);
191 nvme_probe_periph_init()
197 nvme_probe_register(struct cam_periph *periph, void *arg)
199 union ccb *request_ccb; /* CCB representing the probe request */
200 nvme_probe_softc *softc;
202 request_ccb = (union ccb *)arg;
203 if (request_ccb == NULL) {
204 printf("nvme_probe_register: no probe CCB, "
205 "can't register device\n");
206 return(CAM_REQ_CMP_ERR);
209 softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
212 printf("nvme_probe_register: Unable to probe new device. "
213 "Unable to allocate softc\n");
214 return(CAM_REQ_CMP_ERR);
216 TAILQ_INIT(&softc->request_ccbs);
217 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
220 periph->softc = softc;
221 softc->periph = periph;
222 softc->action = NVME_PROBE_INVALID;
223 if (cam_periph_acquire(periph) != 0)
224 return (CAM_REQ_CMP_ERR);
226 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
228 // nvme_device_transport(periph->path);
229 nvme_probe_schedule(periph);
235 nvme_probe_schedule(struct cam_periph *periph)
238 nvme_probe_softc *softc;
240 softc = (nvme_probe_softc *)periph->softc;
241 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
243 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY);
245 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
246 softc->flags |= NVME_PROBE_NO_ANNOUNCE;
248 softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
250 xpt_schedule(periph, CAM_PRIORITY_XPT);
254 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
256 struct ccb_nvmeio *nvmeio;
257 struct ccb_scsiio *csio;
258 nvme_probe_softc *softc;
259 struct cam_path *path;
260 const struct nvme_namespace_data *nvme_data;
263 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
265 softc = (nvme_probe_softc *)periph->softc;
266 path = start_ccb->ccb_h.path;
267 nvmeio = &start_ccb->nvmeio;
268 csio = &start_ccb->csio;
269 nvme_data = periph->path->device->nvme_data;
271 if (softc->restart) {
273 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
274 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_RESET);
276 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY);
280 * Other transports have to ask their SIM to do a lot of action.
281 * NVMe doesn't, so don't do the dance. Just do things
284 switch (softc->action) {
285 case NVME_PROBE_RESET:
287 case NVME_PROBE_IDENTIFY:
288 nvme_device_transport(path);
290 * Test for lun == CAM_LUN_WILDCARD is lame, but
291 * appears to be necessary here. XXX
293 lun = xpt_path_lun_id(periph->path);
294 if (lun == CAM_LUN_WILDCARD ||
295 periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
296 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
297 xpt_acquire_device(path->device);
298 start_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
299 xpt_action(start_ccb);
300 xpt_async(AC_FOUND_DEVICE, path, start_ccb);
302 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
305 panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
308 * Probing is now done. We need to complete any lingering items
309 * in the queue, though there shouldn't be any.
311 xpt_release_ccb(start_ccb);
312 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
313 while ((start_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
314 TAILQ_REMOVE(&softc->request_ccbs,
315 &start_ccb->ccb_h, periph_links.tqe);
316 start_ccb->ccb_h.status = CAM_REQ_CMP;
319 cam_periph_invalidate(periph);
320 cam_periph_release_locked(periph);
324 nvme_probe_cleanup(struct cam_periph *periph)
327 free(periph->softc, M_CAMXPT);
331 /* XXX should be used, don't delete */
333 nvme_find_quirk(struct cam_ed *device)
335 struct nvme_quirk_entry *quirk;
338 match = cam_quirkmatch((caddr_t)&device->nvme_data,
339 (caddr_t)nvme_quirk_table,
340 nvme_quirk_table_size,
341 sizeof(*nvme_quirk_table), nvme_identify_match);
344 panic("xpt_find_quirk: device didn't match wildcard entry!!");
346 quirk = (struct nvme_quirk_entry *)match;
347 device->quirk = quirk;
348 if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
349 device->mintags = quirk->mintags;
350 device->maxtags = quirk->maxtags;
356 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
357 cam_flags flags, union ccb *request_ccb)
359 struct ccb_pathinq cpi;
361 struct cam_periph *old_periph;
364 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
366 xpt_path_inq(&cpi, path);
368 if (cpi.ccb_h.status != CAM_REQ_CMP) {
369 if (request_ccb != NULL) {
370 request_ccb->ccb_h.status = cpi.ccb_h.status;
371 xpt_done(request_ccb);
376 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
377 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
378 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
379 xpt_done(request_ccb);
383 lock = (xpt_path_owned(path) == 0);
386 if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
387 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
388 nvme_probe_softc *softc;
390 softc = (nvme_probe_softc *)old_periph->softc;
391 TAILQ_INSERT_TAIL(&softc->request_ccbs,
392 &request_ccb->ccb_h, periph_links.tqe);
394 CAM_DEBUG(path, CAM_DEBUG_TRACE,
395 ("restarting nvme_probe device\n"));
397 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
398 CAM_DEBUG(path, CAM_DEBUG_TRACE,
399 ("Failing to restart nvme_probe device\n"));
400 xpt_done(request_ccb);
403 CAM_DEBUG(path, CAM_DEBUG_TRACE,
404 ("Adding nvme_probe device\n"));
405 status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
406 nvme_probe_start, "nvme_probe",
408 request_ccb->ccb_h.path, NULL, 0,
411 if (status != CAM_REQ_CMP) {
412 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
413 "returned an error, can't continue probe\n");
414 request_ccb->ccb_h.status = status;
415 xpt_done(request_ccb);
419 xpt_path_unlock(path);
422 static struct cam_ed *
423 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
425 struct nvme_quirk_entry *quirk;
426 struct cam_ed *device;
428 device = xpt_alloc_device(bus, target, lun_id);
433 * Take the default quirk entry until we have inquiry
434 * data from nvme and can determine a better quirk to use.
436 quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
437 device->quirk = (void *)quirk;
440 device->inq_flags = 0;
441 device->queue_flags = 0;
442 device->device_id = NULL; /* XXX Need to set this somewhere */
443 device->device_id_len = 0;
444 device->serial_num = NULL; /* XXX Need to set this somewhere */
445 device->serial_num_len = 0;
450 nvme_device_transport(struct cam_path *path)
452 struct ccb_pathinq cpi;
453 struct ccb_trans_settings cts;
454 /* XXX get data from nvme namespace and other info ??? */
456 /* Get transport information from the SIM */
457 xpt_path_inq(&cpi, path);
459 path->device->transport = cpi.transport;
460 path->device->transport_version = cpi.transport_version;
462 path->device->protocol = cpi.protocol;
463 path->device->protocol_version = cpi.protocol_version;
465 /* Tell the controller what we think */
466 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
467 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
468 cts.type = CTS_TYPE_CURRENT_SETTINGS;
469 cts.transport = path->device->transport;
470 cts.transport_version = path->device->transport_version;
471 cts.protocol = path->device->protocol;
472 cts.protocol_version = path->device->protocol_version;
473 cts.proto_specific.valid = 0;
474 cts.xport_specific.valid = 0;
475 xpt_action((union ccb *)&cts);
479 nvme_dev_advinfo(union ccb *start_ccb)
481 struct cam_ed *device;
482 struct ccb_dev_advinfo *cdai;
485 start_ccb->ccb_h.status = CAM_REQ_INVALID;
486 device = start_ccb->ccb_h.path->device;
487 cdai = &start_ccb->cdai;
488 switch(cdai->buftype) {
489 case CDAI_TYPE_SCSI_DEVID:
490 if (cdai->flags & CDAI_FLAG_STORE)
492 cdai->provsiz = device->device_id_len;
493 if (device->device_id_len == 0)
495 amt = device->device_id_len;
496 if (cdai->provsiz > cdai->bufsiz)
498 memcpy(cdai->buf, device->device_id, amt);
500 case CDAI_TYPE_SERIAL_NUM:
501 if (cdai->flags & CDAI_FLAG_STORE)
503 cdai->provsiz = device->serial_num_len;
504 if (device->serial_num_len == 0)
506 amt = device->serial_num_len;
507 if (cdai->provsiz > cdai->bufsiz)
509 memcpy(cdai->buf, device->serial_num, amt);
511 case CDAI_TYPE_PHYS_PATH:
512 if (cdai->flags & CDAI_FLAG_STORE) {
513 if (device->physpath != NULL)
514 free(device->physpath, M_CAMXPT);
515 device->physpath_len = cdai->bufsiz;
516 /* Clear existing buffer if zero length */
517 if (cdai->bufsiz == 0)
519 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
520 if (device->physpath == NULL) {
521 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
524 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
526 cdai->provsiz = device->physpath_len;
527 if (device->physpath_len == 0)
529 amt = device->physpath_len;
530 if (cdai->provsiz > cdai->bufsiz)
532 memcpy(cdai->buf, device->physpath, amt);
535 case CDAI_TYPE_NVME_CNTRL:
536 if (cdai->flags & CDAI_FLAG_STORE)
538 amt = sizeof(struct nvme_controller_data);
540 if (amt > cdai->bufsiz)
542 memcpy(cdai->buf, device->nvme_cdata, amt);
544 case CDAI_TYPE_NVME_NS:
545 if (cdai->flags & CDAI_FLAG_STORE)
547 amt = sizeof(struct nvme_namespace_data);
549 if (amt > cdai->bufsiz)
551 memcpy(cdai->buf, device->nvme_data, amt);
556 start_ccb->ccb_h.status = CAM_REQ_CMP;
558 if (cdai->flags & CDAI_FLAG_STORE) {
559 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
560 (void *)(uintptr_t)cdai->buftype);
565 nvme_action(union ccb *start_ccb)
567 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
568 ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
570 switch (start_ccb->ccb_h.func_code) {
574 nvme_scan_lun(start_ccb->ccb_h.path->periph,
575 start_ccb->ccb_h.path, start_ccb->crcn.flags,
578 case XPT_DEV_ADVINFO:
579 nvme_dev_advinfo(start_ccb);
583 xpt_action_default(start_ccb);
589 * Handle any per-device event notifications that require action by the XPT.
592 nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
593 struct cam_ed *device, void *async_arg)
597 * We only need to handle events for real devices.
599 if (target->target_id == CAM_TARGET_WILDCARD
600 || device->lun_id == CAM_LUN_WILDCARD)
603 if (async_code == AC_LOST_DEVICE &&
604 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
605 device->flags |= CAM_DEV_UNCONFIGURED;
606 xpt_release_device(device);
611 nvme_announce_periph(struct cam_periph *periph)
613 struct ccb_pathinq cpi;
614 struct ccb_trans_settings cts;
615 struct cam_path *path = periph->path;
616 struct ccb_trans_settings_nvme *nvmex;
618 cam_periph_assert(periph, MA_OWNED);
620 /* Ask the SIM for connection details */
621 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
622 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
623 cts.type = CTS_TYPE_CURRENT_SETTINGS;
624 xpt_action((union ccb*)&cts);
625 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
627 nvmex = &cts.xport_specific.nvme;
629 /* Ask the SIM for its base transfer speed */
630 xpt_path_inq(&cpi, periph->path);
631 printf("%s%d: nvme version %d.%d x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
632 periph->periph_name, periph->unit_number,
633 NVME_MAJOR(nvmex->spec),
634 NVME_MINOR(nvmex->spec),
635 nvmex->lanes, nvmex->max_lanes,
636 nvmex->speed, nvmex->max_speed);
641 nvme_proto_announce(struct cam_ed *device)
646 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
647 nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
653 nvme_proto_denounce(struct cam_ed *device)
656 nvme_proto_announce(device);
660 nvme_proto_debug_out(union ccb *ccb)
662 char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
664 if (ccb->ccb_h.func_code != XPT_NVME_IO)
667 CAM_DEBUG(ccb->ccb_h.path,
668 CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd),
669 nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));