2 * Copyright (c) 2015 Netflix, Inc.
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 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/cam_xpt_sim.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_xpt_internal.h>
58 #include <cam/cam_debug.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/nvme/nvme_all.h>
63 #include <machine/stdarg.h> /* for xpt_print below */
66 struct nvme_quirk_entry {
68 #define CAM_QUIRK_MAXTAGS 1
73 /* Not even sure why we need this */
74 static periph_init_t nvme_probe_periph_init;
76 static struct periph_driver nvme_probe_driver =
78 nvme_probe_periph_init, "nvme_probe",
79 TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
83 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
92 static char *nvme_probe_action_text[] = {
93 "NVME_PROBE_IDENTIFY",
99 #define NVME_PROBE_SET_ACTION(softc, newaction) \
102 text = nvme_probe_action_text; \
103 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
104 ("Probe %s to %s\n", text[(softc)->action], \
105 text[(newaction)])); \
106 (softc)->action = (newaction); \
110 NVME_PROBE_NO_ANNOUNCE = 0x04
114 TAILQ_HEAD(, ccb_hdr) request_ccbs;
115 nvme_probe_action action;
116 nvme_probe_flags flags;
118 struct cam_periph *periph;
121 static struct nvme_quirk_entry nvme_quirk_table[] =
125 // T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
126 // /*vendor*/"*", /*product*/"*", /*revision*/"*"
128 .quirks = 0, .mintags = 0, .maxtags = 0
132 static const int nvme_quirk_table_size =
133 sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
135 static cam_status nvme_probe_register(struct cam_periph *periph,
137 static void nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
138 static void nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
139 static void nvme_probe_cleanup(struct cam_periph *periph);
140 //static void nvme_find_quirk(struct cam_ed *device);
141 static void nvme_scan_lun(struct cam_periph *periph,
142 struct cam_path *path, cam_flags flags,
144 static struct cam_ed *
145 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
147 static void nvme_device_transport(struct cam_path *path);
148 static void nvme_dev_async(u_int32_t async_code,
150 struct cam_et *target,
151 struct cam_ed *device,
153 static void nvme_action(union ccb *start_ccb);
154 static void nvme_announce_periph(struct cam_periph *periph);
156 static struct xpt_xport nvme_xport = {
157 .alloc_device = nvme_alloc_device,
158 .action = nvme_action,
159 .async = nvme_dev_async,
160 .announce = nvme_announce_periph,
166 return (&nvme_xport);
170 nvme_probe_periph_init()
172 printf("nvme cam probe device init\n");
176 nvme_probe_register(struct cam_periph *periph, void *arg)
178 union ccb *request_ccb; /* CCB representing the probe request */
180 nvme_probe_softc *softc;
182 request_ccb = (union ccb *)arg;
183 if (request_ccb == NULL) {
184 printf("nvme_probe_register: no probe CCB, "
185 "can't register device\n");
186 return(CAM_REQ_CMP_ERR);
189 softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
192 printf("nvme_probe_register: Unable to probe new device. "
193 "Unable to allocate softc\n");
194 return(CAM_REQ_CMP_ERR);
196 TAILQ_INIT(&softc->request_ccbs);
197 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
200 periph->softc = softc;
201 softc->periph = periph;
202 softc->action = NVME_PROBE_INVALID;
203 status = cam_periph_acquire(periph);
204 if (status != CAM_REQ_CMP) {
207 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
209 // nvme_device_transport(periph->path);
210 nvme_probe_schedule(periph);
216 nvme_probe_schedule(struct cam_periph *periph)
219 nvme_probe_softc *softc;
221 softc = (nvme_probe_softc *)periph->softc;
222 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
224 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY);
226 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
227 softc->flags |= NVME_PROBE_NO_ANNOUNCE;
229 softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
231 xpt_schedule(periph, CAM_PRIORITY_XPT);
235 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
237 struct ccb_nvmeio *nvmeio;
238 struct ccb_scsiio *csio;
239 nvme_probe_softc *softc;
240 struct cam_path *path;
241 const struct nvme_namespace_data *nvme_data;
244 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
246 softc = (nvme_probe_softc *)periph->softc;
247 path = start_ccb->ccb_h.path;
248 nvmeio = &start_ccb->nvmeio;
249 csio = &start_ccb->csio;
250 nvme_data = periph->path->device->nvme_data;
252 if (softc->restart) {
254 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
255 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_RESET);
257 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY);
261 * Other transports have to ask their SIM to do a lot of action.
262 * NVMe doesn't, so don't do the dance. Just do things
265 switch (softc->action) {
266 case NVME_PROBE_RESET:
268 case NVME_PROBE_IDENTIFY:
269 nvme_device_transport(path);
271 * Test for lun == CAM_LUN_WILDCARD is lame, but
272 * appears to be necessary here. XXX
274 lun = xpt_path_lun_id(periph->path);
275 if (lun == CAM_LUN_WILDCARD ||
276 periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
277 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
278 xpt_acquire_device(path->device);
279 start_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
280 xpt_action(start_ccb);
281 xpt_async(AC_FOUND_DEVICE, path, start_ccb);
283 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
286 panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
289 * Probing is now done. We need to complete any lingering items
290 * in the queue, though there shouldn't be any.
292 xpt_release_ccb(start_ccb);
293 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
294 while ((start_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
295 TAILQ_REMOVE(&softc->request_ccbs,
296 &start_ccb->ccb_h, periph_links.tqe);
297 start_ccb->ccb_h.status = CAM_REQ_CMP;
300 // XXX not sure I need this
301 // XXX unlike other XPTs, we never freeze the queue since we have a super-simple
303 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. -- did we really do this? */
304 // cam_release_devq(path, 0, 0, 0, FALSE);
305 cam_periph_invalidate(periph);
306 cam_periph_release_locked(periph);
310 nvme_probe_cleanup(struct cam_periph *periph)
312 free(periph->softc, M_CAMXPT);
316 /* XXX should be used, don't delete */
318 nvme_find_quirk(struct cam_ed *device)
320 struct nvme_quirk_entry *quirk;
323 match = cam_quirkmatch((caddr_t)&device->nvme_data,
324 (caddr_t)nvme_quirk_table,
325 nvme_quirk_table_size,
326 sizeof(*nvme_quirk_table), nvme_identify_match);
329 panic("xpt_find_quirk: device didn't match wildcard entry!!");
331 quirk = (struct nvme_quirk_entry *)match;
332 device->quirk = quirk;
333 if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
334 device->mintags = quirk->mintags;
335 device->maxtags = quirk->maxtags;
341 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
342 cam_flags flags, union ccb *request_ccb)
344 struct ccb_pathinq cpi;
346 struct cam_periph *old_periph;
349 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
351 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
352 cpi.ccb_h.func_code = XPT_PATH_INQ;
353 xpt_action((union ccb *)&cpi);
355 if (cpi.ccb_h.status != CAM_REQ_CMP) {
356 if (request_ccb != NULL) {
357 request_ccb->ccb_h.status = cpi.ccb_h.status;
358 xpt_done(request_ccb);
363 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
364 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
365 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
366 xpt_done(request_ccb);
370 lock = (xpt_path_owned(path) == 0);
373 if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
374 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
375 nvme_probe_softc *softc;
377 softc = (nvme_probe_softc *)old_periph->softc;
378 TAILQ_INSERT_TAIL(&softc->request_ccbs,
379 &request_ccb->ccb_h, periph_links.tqe);
381 CAM_DEBUG(path, CAM_DEBUG_TRACE,
382 ("restarting nvme_probe device\n"));
384 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
385 CAM_DEBUG(path, CAM_DEBUG_TRACE,
386 ("Failing to restart nvme_probe device\n"));
387 xpt_done(request_ccb);
390 CAM_DEBUG(path, CAM_DEBUG_TRACE,
391 ("Adding nvme_probe device\n"));
392 status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
393 nvme_probe_start, "nvme_probe",
395 request_ccb->ccb_h.path, NULL, 0,
398 if (status != CAM_REQ_CMP) {
399 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
400 "returned an error, can't continue probe\n");
401 request_ccb->ccb_h.status = status;
402 xpt_done(request_ccb);
406 xpt_path_unlock(path);
409 static struct cam_ed *
410 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
412 struct nvme_quirk_entry *quirk;
413 struct cam_ed *device;
415 device = xpt_alloc_device(bus, target, lun_id);
420 * Take the default quirk entry until we have inquiry
421 * data from nvme and can determine a better quirk to use.
423 quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
424 device->quirk = (void *)quirk;
427 device->inq_flags = 0;
428 device->queue_flags = 0;
429 device->device_id = NULL; /* XXX Need to set this somewhere */
430 device->device_id_len = 0;
431 device->serial_num = NULL; /* XXX Need to set this somewhere */
432 device->serial_num_len = 0;
437 nvme_device_transport(struct cam_path *path)
439 struct ccb_pathinq cpi;
440 struct ccb_trans_settings cts;
441 /* XXX get data from nvme namespace and other info ??? */
443 /* Get transport information from the SIM */
444 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
445 cpi.ccb_h.func_code = XPT_PATH_INQ;
446 xpt_action((union ccb *)&cpi);
448 path->device->transport = cpi.transport;
449 path->device->transport_version = cpi.transport_version;
451 path->device->protocol = cpi.protocol;
452 path->device->protocol_version = cpi.protocol_version;
454 /* Tell the controller what we think */
455 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
456 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
457 cts.type = CTS_TYPE_CURRENT_SETTINGS;
458 cts.transport = path->device->transport;
459 cts.transport_version = path->device->transport_version;
460 cts.protocol = path->device->protocol;
461 cts.protocol_version = path->device->protocol_version;
462 cts.proto_specific.valid = 0;
463 cts.xport_specific.valid = 0;
464 xpt_action((union ccb *)&cts);
468 nvme_dev_advinfo(union ccb *start_ccb)
470 struct cam_ed *device;
471 struct ccb_dev_advinfo *cdai;
474 start_ccb->ccb_h.status = CAM_REQ_INVALID;
475 device = start_ccb->ccb_h.path->device;
476 cdai = &start_ccb->cdai;
477 switch(cdai->buftype) {
478 case CDAI_TYPE_SCSI_DEVID:
479 if (cdai->flags & CDAI_FLAG_STORE)
481 cdai->provsiz = device->device_id_len;
482 if (device->device_id_len == 0)
484 amt = device->device_id_len;
485 if (cdai->provsiz > cdai->bufsiz)
487 memcpy(cdai->buf, device->device_id, amt);
489 case CDAI_TYPE_SERIAL_NUM:
490 if (cdai->flags & CDAI_FLAG_STORE)
492 cdai->provsiz = device->serial_num_len;
493 if (device->serial_num_len == 0)
495 amt = device->serial_num_len;
496 if (cdai->provsiz > cdai->bufsiz)
498 memcpy(cdai->buf, device->serial_num, amt);
500 case CDAI_TYPE_PHYS_PATH:
501 if (cdai->flags & CDAI_FLAG_STORE) {
502 if (device->physpath != NULL)
503 free(device->physpath, M_CAMXPT);
504 device->physpath_len = cdai->bufsiz;
505 /* Clear existing buffer if zero length */
506 if (cdai->bufsiz == 0)
508 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
509 if (device->physpath == NULL) {
510 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
513 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
515 cdai->provsiz = device->physpath_len;
516 if (device->physpath_len == 0)
518 amt = device->physpath_len;
519 if (cdai->provsiz > cdai->bufsiz)
521 memcpy(cdai->buf, device->physpath, amt);
527 start_ccb->ccb_h.status = CAM_REQ_CMP;
529 if (cdai->flags & CDAI_FLAG_STORE) {
530 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
531 (void *)(uintptr_t)cdai->buftype);
536 nvme_action(union ccb *start_ccb)
538 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
539 ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
541 switch (start_ccb->ccb_h.func_code) {
543 printf("NVME scan BUS started -- ignored\n");
546 printf("NVME scan TGT started -- ignored\n");
549 printf("NVME scan started\n");
550 nvme_scan_lun(start_ccb->ccb_h.path->periph,
551 start_ccb->ccb_h.path, start_ccb->crcn.flags,
554 case XPT_DEV_ADVINFO:
555 nvme_dev_advinfo(start_ccb);
559 xpt_action_default(start_ccb);
565 * Handle any per-device event notifications that require action by the XPT.
568 nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
569 struct cam_ed *device, void *async_arg)
573 * We only need to handle events for real devices.
575 if (target->target_id == CAM_TARGET_WILDCARD
576 || device->lun_id == CAM_LUN_WILDCARD)
579 if (async_code == AC_LOST_DEVICE &&
580 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
581 device->flags |= CAM_DEV_UNCONFIGURED;
582 xpt_release_device(device);
587 nvme_announce_periph(struct cam_periph *periph)
589 struct ccb_pathinq cpi;
590 struct ccb_trans_settings cts;
591 struct cam_path *path = periph->path;
593 cam_periph_assert(periph, MA_OWNED);
595 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
596 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
597 cts.type = CTS_TYPE_CURRENT_SETTINGS;
598 xpt_action((union ccb*)&cts);
599 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
601 /* Ask the SIM for its base transfer speed */
602 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
603 cpi.ccb_h.func_code = XPT_PATH_INQ;
604 xpt_action((union ccb *)&cpi);
605 /* XXX NVME STUFF HERE */