FreeBSD kernel CAM code
nvme_xpt.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2015 Netflix, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
15 *
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.
26 *
27 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/param.h>
34#include <sys/bus.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>
40#include <sys/time.h>
41#include <sys/conf.h>
42#include <sys/fcntl.h>
43#include <sys/sbuf.h>
44
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/sysctl.h>
48
49#include <cam/cam.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>
58#include <cam/cam_debug.h>
59
60#include <cam/scsi/scsi_all.h>
62#include <cam/nvme/nvme_all.h>
63#include <machine/stdarg.h> /* for xpt_print below */
64#include "opt_cam.h"
65
67 u_int quirks;
68#define CAM_QUIRK_MAXTAGS 1
69 u_int mintags;
70 u_int maxtags;
71};
72
73/* Not even sure why we need this */
75
77{
78 nvme_probe_periph_init, "nvme_probe",
79 TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
81};
82
84
85typedef enum {
91
92static char *nvme_probe_action_text[] = {
93 "NVME_PROBE_IDENTIFY_CD",
94 "NVME_PROBE_IDENTIFY_NS",
95 "NVME_PROBE_DONE",
96 "NVME_PROBE_INVALID"
97};
98
99#define NVME_PROBE_SET_ACTION(softc, newaction) \
100do { \
101 char **text; \
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); \
107} while(0)
108
109typedef enum {
112
113typedef struct {
114 TAILQ_HEAD(, ccb_hdr) request_ccbs;
115 union {
116 struct nvme_controller_data cd;
117 struct nvme_namespace_data ns;
118 };
124
126{
127 {
128// {
129// T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
130// /*vendor*/"*", /*product*/"*", /*revision*/"*"
131// },
132 .quirks = 0, .mintags = 0, .maxtags = 0
133 },
134};
135
136static const int nvme_quirk_table_size =
137 sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
138
139static cam_status nvme_probe_register(struct cam_periph *periph,
140 void *arg);
141static void nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
142static void nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
143static void nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb);
144static void nvme_probe_cleanup(struct cam_periph *periph);
145//static void nvme_find_quirk(struct cam_ed *device);
146static void nvme_scan_lun(struct cam_periph *periph,
147 struct cam_path *path, cam_flags flags,
148 union ccb *ccb);
149static struct cam_ed *
150 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
152static void nvme_device_transport(struct cam_path *path);
153static void nvme_dev_async(u_int32_t async_code,
154 struct cam_eb *bus,
155 struct cam_et *target,
156 struct cam_ed *device,
157 void *async_arg);
158static void nvme_action(union ccb *start_ccb);
159static void nvme_announce_periph(struct cam_periph *periph);
160static void nvme_proto_announce(struct cam_ed *device);
161static void nvme_proto_denounce(struct cam_ed *device);
162static void nvme_proto_debug_out(union ccb *ccb);
163
166 .action = nvme_action,
167 .async = nvme_dev_async,
168 .announce = nvme_announce_periph,
169};
170#define NVME_XPT_XPORT(x, X) \
171static struct xpt_xport nvme_xport_ ## x = { \
172 .xport = XPORT_ ## X, \
173 .name = #x, \
174 .ops = &nvme_xport_ops, \
175}; \
176CAM_XPT_XPORT(nvme_xport_ ## x);
177
178NVME_XPT_XPORT(nvme, NVME);
179
180#undef NVME_XPT_XPORT
181
184 .denounce = nvme_proto_denounce,
185 .debug_out = nvme_proto_debug_out,
186};
187static struct xpt_proto nvme_proto = {
188 .proto = PROTO_NVME,
189 .name = "nvme",
190 .ops = &nvme_proto_ops,
191};
193
194static void
196{
197}
198
199static cam_status
200nvme_probe_register(struct cam_periph *periph, void *arg)
201{
202 union ccb *request_ccb; /* CCB representing the probe request */
203 nvme_probe_softc *softc;
204
205 request_ccb = (union ccb *)arg;
206 if (request_ccb == NULL) {
207 printf("nvme_probe_register: no probe CCB, "
208 "can't register device\n");
209 return(CAM_REQ_CMP_ERR);
210 }
211
212 softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
213
214 if (softc == NULL) {
215 printf("nvme_probe_register: Unable to probe new device. "
216 "Unable to allocate softc\n");
217 return(CAM_REQ_CMP_ERR);
218 }
219 TAILQ_INIT(&softc->request_ccbs);
220 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
221 periph_links.tqe);
222 softc->flags = 0;
223 periph->softc = softc;
224 softc->periph = periph;
225 softc->action = NVME_PROBE_INVALID;
226 if (cam_periph_acquire(periph) != 0)
227 return (CAM_REQ_CMP_ERR);
228
229 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
230
231// nvme_device_transport(periph->path);
232 nvme_probe_schedule(periph);
233
234 return(CAM_REQ_CMP);
235}
236
237static void
239{
240 union ccb *ccb;
241 nvme_probe_softc *softc;
242
243 softc = (nvme_probe_softc *)periph->softc;
244 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
245
247
250 else
251 softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
252
254}
255
256static void
257nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
258{
259 struct ccb_nvmeio *nvmeio;
260 nvme_probe_softc *softc;
261 lun_id_t lun;
262
263 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
264
265 softc = (nvme_probe_softc *)periph->softc;
266 nvmeio = &start_ccb->nvmeio;
267 lun = xpt_path_lun_id(periph->path);
268
269 if (softc->restart) {
270 softc->restart = 0;
272 }
273
274 switch (softc->action) {
276 cam_fill_nvmeadmin(nvmeio,
277 0, /* retries */
278 nvme_probe_done, /* cbfcnp */
279 CAM_DIR_IN, /* flags */
280 (uint8_t *)&softc->cd, /* data_ptr */
281 sizeof(softc->cd), /* dxfer_len */
282 30 * 1000); /* timeout 30s */
283 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0,
284 1, 0, 0, 0, 0, 0);
285 break;
287 cam_fill_nvmeadmin(nvmeio,
288 0, /* retries */
289 nvme_probe_done, /* cbfcnp */
290 CAM_DIR_IN, /* flags */
291 (uint8_t *)&softc->ns, /* data_ptr */
292 sizeof(softc->ns), /* dxfer_len */
293 30 * 1000); /* timeout 30s */
294 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun,
295 0, 0, 0, 0, 0, 0);
296 break;
297 default:
298 panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
299 }
300 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
301 xpt_action(start_ccb);
302}
303
304static void
305nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
306{
307 struct nvme_namespace_data *nvme_data;
308 struct nvme_controller_data *nvme_cdata;
309 nvme_probe_softc *softc;
310 struct cam_path *path;
311 struct scsi_vpd_device_id *did;
312 struct scsi_vpd_id_descriptor *idd;
313 u_int32_t priority;
314 int found = 1, e, g, len;
315
316 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n"));
317
318 softc = (nvme_probe_softc *)periph->softc;
319 path = done_ccb->ccb_h.path;
320 priority = done_ccb->ccb_h.pinfo.priority;
321
322 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
323 if (cam_periph_error(done_ccb,
324 0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
325 ) == ERESTART) {
326out:
327 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
328 cam_release_devq(path, 0, 0, 0, FALSE);
329 return;
330 }
331 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
332 /* Don't wedge the queue */
333 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
334 }
335
336 /*
337 * If we get to this point, we got an error status back
338 * from the inquiry and the error status doesn't require
339 * automatically retrying the command. Therefore, the
340 * inquiry failed. If we had inquiry information before
341 * for this device, but this latest inquiry command failed,
342 * the device has probably gone away. If this device isn't
343 * already marked unconfigured, notify the peripheral
344 * drivers that this device is no more.
345 */
346device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
347 xpt_async(AC_LOST_DEVICE, path, NULL);
349 found = 0;
350 goto done;
351 }
352 if (softc->restart)
353 goto done;
354 switch (softc->action) {
356 nvme_controller_data_swapbytes(&softc->cd);
357
358 nvme_cdata = path->device->nvme_cdata;
359 if (nvme_cdata == NULL) {
360 nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT,
361 M_NOWAIT);
362 if (nvme_cdata == NULL) {
363 xpt_print(path, "Can't allocate memory");
364 goto device_fail;
365 }
366 }
367 bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata));
368 path->device->nvme_cdata = nvme_cdata;
369
370 /* Save/update serial number. */
371 if (path->device->serial_num != NULL) {
372 free(path->device->serial_num, M_CAMXPT);
373 path->device->serial_num = NULL;
374 path->device->serial_num_len = 0;
375 }
376 path->device->serial_num = (u_int8_t *)
377 malloc(NVME_SERIAL_NUMBER_LENGTH + 1, M_CAMXPT, M_NOWAIT);
378 if (path->device->serial_num != NULL) {
380 nvme_cdata->sn, sizeof(nvme_cdata->sn),
381 NVME_SERIAL_NUMBER_LENGTH + 1,
383
384 path->device->serial_num_len =
385 strlen(path->device->serial_num);
386 }
387
388// nvme_find_quirk(path->device);
391 xpt_release_ccb(done_ccb);
392 xpt_schedule(periph, priority);
393 goto out;
395 nvme_namespace_data_swapbytes(&softc->ns);
396
397 /* Check that the namespace exists. */
398 if (softc->ns.nsze == 0)
399 goto device_fail;
400
401 nvme_data = path->device->nvme_data;
402 if (nvme_data == NULL) {
403 nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
404 M_NOWAIT);
405 if (nvme_data == NULL) {
406 xpt_print(path, "Can't allocate memory");
407 goto device_fail;
408 }
409 }
410 bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
411 path->device->nvme_data = nvme_data;
412
413 /* Save/update device_id based on NGUID and/or EUI64. */
414 if (path->device->device_id != NULL) {
415 free(path->device->device_id, M_CAMXPT);
416 path->device->device_id = NULL;
417 path->device->device_id_len = 0;
418 }
419 len = 0;
420 for (g = 0; g < sizeof(nvme_data->nguid); g++) {
421 if (nvme_data->nguid[g] != 0)
422 break;
423 }
424 if (g < sizeof(nvme_data->nguid))
425 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
426 for (e = 0; e < sizeof(nvme_data->eui64); e++) {
427 if (nvme_data->eui64[e] != 0)
428 break;
429 }
430 if (e < sizeof(nvme_data->eui64))
431 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
432 if (len > 0) {
433 path->device->device_id = (u_int8_t *)
434 malloc(SVPD_DEVICE_ID_HDR_LEN + len,
435 M_CAMXPT, M_NOWAIT);
436 }
437 if (path->device->device_id != NULL) {
438 did = (struct scsi_vpd_device_id *)path->device->device_id;
441 scsi_ulto2b(len, did->length);
442 idd = (struct scsi_vpd_id_descriptor *)(did + 1);
443 if (g < sizeof(nvme_data->nguid)) {
446 idd->length = 16;
447 bcopy(nvme_data->nguid, idd->identifier, 16);
448 idd = (struct scsi_vpd_id_descriptor *)
449 &idd->identifier[16];
450 }
451 if (e < sizeof(nvme_data->eui64)) {
454 idd->length = 8;
455 bcopy(nvme_data->eui64, idd->identifier, 8);
456 }
458 }
459
460 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
461 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
463 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
464 xpt_action(done_ccb);
465 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
466 }
468 break;
469 default:
470 panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
471 }
472done:
473 if (softc->restart) {
474 softc->restart = 0;
475 xpt_release_ccb(done_ccb);
476 nvme_probe_schedule(periph);
477 goto out;
478 }
479 xpt_release_ccb(done_ccb);
480 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
481 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
482 TAILQ_REMOVE(&softc->request_ccbs,
483 &done_ccb->ccb_h, periph_links.tqe);
484 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
485 xpt_done(done_ccb);
486 }
487 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
488 cam_release_devq(path, 0, 0, 0, FALSE);
489 cam_periph_invalidate(periph);
491}
492
493static void
495{
496
497 free(periph->softc, M_CAMXPT);
498}
499
500#if 0
501/* XXX should be used, don't delete */
502static void
503nvme_find_quirk(struct cam_ed *device)
504{
505 struct nvme_quirk_entry *quirk;
506 caddr_t match;
507
508 match = cam_quirkmatch((caddr_t)&device->nvme_data,
509 (caddr_t)nvme_quirk_table,
512
513 if (match == NULL)
514 panic("xpt_find_quirk: device didn't match wildcard entry!!");
515
516 quirk = (struct nvme_quirk_entry *)match;
517 device->quirk = quirk;
518 if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
519 device->mintags = quirk->mintags;
520 device->maxtags = quirk->maxtags;
521 }
522}
523#endif
524
525static void
526nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
527 cam_flags flags, union ccb *request_ccb)
528{
529 struct ccb_pathinq cpi;
530 cam_status status;
531 struct cam_periph *old_periph;
532 int lock;
533
534 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
535
536 xpt_path_inq(&cpi, path);
537
538 if (cpi.ccb_h.status != CAM_REQ_CMP) {
539 if (request_ccb != NULL) {
540 request_ccb->ccb_h.status = cpi.ccb_h.status;
541 xpt_done(request_ccb);
542 }
543 return;
544 }
545
547 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
548 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
549 xpt_done(request_ccb);
550 return;
551 }
552
553 lock = (xpt_path_owned(path) == 0);
554 if (lock)
556 if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
557 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
559
560 softc = (nvme_probe_softc *)old_periph->softc;
561 TAILQ_INSERT_TAIL(&softc->request_ccbs,
562 &request_ccb->ccb_h, periph_links.tqe);
563 softc->restart = 1;
565 ("restarting nvme_probe device\n"));
566 } else {
567 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
569 ("Failing to restart nvme_probe device\n"));
570 xpt_done(request_ccb);
571 }
572 } else {
574 ("Adding nvme_probe device\n"));
576 nvme_probe_start, "nvme_probe",
578 request_ccb->ccb_h.path, NULL, 0,
579 request_ccb);
580
581 if (status != CAM_REQ_CMP) {
582 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
583 "returned an error, can't continue probe\n");
584 request_ccb->ccb_h.status = status;
585 xpt_done(request_ccb);
586 }
587 }
588 if (lock)
590}
591
592static struct cam_ed *
594{
595 struct nvme_quirk_entry *quirk;
596 struct cam_ed *device;
597
598 device = xpt_alloc_device(bus, target, lun_id);
599 if (device == NULL)
600 return (NULL);
601
602 /*
603 * Take the default quirk entry until we have inquiry
604 * data from nvme and can determine a better quirk to use.
605 */
607 device->quirk = (void *)quirk;
608 device->mintags = 0;
609 device->maxtags = 0;
610 device->inq_flags = 0;
611 device->queue_flags = 0;
612 device->device_id = NULL;
613 device->device_id_len = 0;
614 device->serial_num = NULL;
615 device->serial_num_len = 0;
616 return (device);
617}
618
619static void
621{
622 struct ccb_pathinq cpi;
623 struct ccb_trans_settings cts;
624 /* XXX get data from nvme namespace and other info ??? */
625
626 /* Get transport information from the SIM */
627 xpt_path_inq(&cpi, path);
628
629 path->device->transport = cpi.transport;
631
632 path->device->protocol = cpi.protocol;
634
635 /* Tell the controller what we think */
636 memset(&cts, 0, sizeof(cts));
637 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
638 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
640 cts.transport = path->device->transport;
641 cts.transport_version = path->device->transport_version;
642 cts.protocol = path->device->protocol;
643 cts.protocol_version = path->device->protocol_version;
644 cts.proto_specific.valid = 0;
645 cts.xport_specific.valid = 0;
646 xpt_action((union ccb *)&cts);
647}
648
649static void
650nvme_dev_advinfo(union ccb *start_ccb)
651{
652 struct cam_ed *device;
653 struct ccb_dev_advinfo *cdai;
654 off_t amt;
655
656 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
657 start_ccb->ccb_h.status = CAM_REQ_INVALID;
658 device = start_ccb->ccb_h.path->device;
659 cdai = &start_ccb->cdai;
660 switch(cdai->buftype) {
662 if (cdai->flags & CDAI_FLAG_STORE)
663 return;
664 cdai->provsiz = device->device_id_len;
665 if (device->device_id_len == 0)
666 break;
667 amt = device->device_id_len;
668 if (cdai->provsiz > cdai->bufsiz)
669 amt = cdai->bufsiz;
670 memcpy(cdai->buf, device->device_id, amt);
671 break;
673 if (cdai->flags & CDAI_FLAG_STORE)
674 return;
675 cdai->provsiz = device->serial_num_len;
676 if (device->serial_num_len == 0)
677 break;
678 amt = device->serial_num_len;
679 if (cdai->provsiz > cdai->bufsiz)
680 amt = cdai->bufsiz;
681 memcpy(cdai->buf, device->serial_num, amt);
682 break;
684 if (cdai->flags & CDAI_FLAG_STORE) {
685 if (device->physpath != NULL) {
686 free(device->physpath, M_CAMXPT);
687 device->physpath = NULL;
688 device->physpath_len = 0;
689 }
690 /* Clear existing buffer if zero length */
691 if (cdai->bufsiz == 0)
692 break;
693 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
694 if (device->physpath == NULL) {
695 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
696 return;
697 }
698 device->physpath_len = cdai->bufsiz;
699 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
700 } else {
701 cdai->provsiz = device->physpath_len;
702 if (device->physpath_len == 0)
703 break;
704 amt = device->physpath_len;
705 if (cdai->provsiz > cdai->bufsiz)
706 amt = cdai->bufsiz;
707 memcpy(cdai->buf, device->physpath, amt);
708 }
709 break;
711 if (cdai->flags & CDAI_FLAG_STORE)
712 return;
713 amt = sizeof(struct nvme_controller_data);
714 cdai->provsiz = amt;
715 if (amt > cdai->bufsiz)
716 amt = cdai->bufsiz;
717 memcpy(cdai->buf, device->nvme_cdata, amt);
718 break;
720 if (cdai->flags & CDAI_FLAG_STORE)
721 return;
722 amt = sizeof(struct nvme_namespace_data);
723 cdai->provsiz = amt;
724 if (amt > cdai->bufsiz)
725 amt = cdai->bufsiz;
726 memcpy(cdai->buf, device->nvme_data, amt);
727 break;
728 default:
729 return;
730 }
731 start_ccb->ccb_h.status = CAM_REQ_CMP;
732
733 if (cdai->flags & CDAI_FLAG_STORE) {
735 (void *)(uintptr_t)cdai->buftype);
736 }
737}
738
739static void
740nvme_action(union ccb *start_ccb)
741{
743 ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
744
745 switch (start_ccb->ccb_h.func_code) {
746 case XPT_SCAN_BUS:
747 case XPT_SCAN_TGT:
748 case XPT_SCAN_LUN:
749 nvme_scan_lun(start_ccb->ccb_h.path->periph,
750 start_ccb->ccb_h.path, start_ccb->crcn.flags,
751 start_ccb);
752 break;
753 case XPT_DEV_ADVINFO:
754 nvme_dev_advinfo(start_ccb);
755 break;
756
757 default:
758 xpt_action_default(start_ccb);
759 break;
760 }
761}
762
763/*
764 * Handle any per-device event notifications that require action by the XPT.
765 */
766static void
767nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
768 struct cam_ed *device, void *async_arg)
769{
770
771 /*
772 * We only need to handle events for real devices.
773 */
774 if (target->target_id == CAM_TARGET_WILDCARD
775 || device->lun_id == CAM_LUN_WILDCARD)
776 return;
777
778 if (async_code == AC_LOST_DEVICE &&
779 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
780 device->flags |= CAM_DEV_UNCONFIGURED;
781 xpt_release_device(device);
782 }
783}
784
785static void
787{
788 struct ccb_pathinq cpi;
789 struct ccb_trans_settings cts;
790 struct cam_path *path = periph->path;
791 struct ccb_trans_settings_nvme *nvmex;
792 struct sbuf sb;
793 char buffer[120];
794
795 cam_periph_assert(periph, MA_OWNED);
796
797 /* Ask the SIM for connection details */
798 memset(&cts, 0, sizeof(cts));
800 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
802 xpt_action((union ccb*)&cts);
803 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
804 return;
805 nvmex = &cts.xport_specific.nvme;
806
807 /* Ask the SIM for its base transfer speed */
808 xpt_path_inq(&cpi, periph->path);
809 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
810 sbuf_printf(&sb, "%s%d: nvme version %d.%d",
811 periph->periph_name, periph->unit_number,
812 NVME_MAJOR(nvmex->spec),
813 NVME_MINOR(nvmex->spec));
814 if (nvmex->valid & CTS_NVME_VALID_LINK)
815 sbuf_printf(&sb, " x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
816 nvmex->lanes, nvmex->max_lanes,
817 nvmex->speed, nvmex->max_speed);
818 sbuf_printf(&sb, "\n");
819 sbuf_finish(&sb);
820 sbuf_putbuf(&sb);
821}
822
823static void
825{
826 struct sbuf sb;
827 char buffer[120];
828
829 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
830 nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
831 sbuf_finish(&sb);
832 sbuf_putbuf(&sb);
833}
834
835static void
837{
838
839 nvme_proto_announce(device);
840}
841
842static void
844{
845 char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
846
847 if (ccb->ccb_h.func_code != XPT_NVME_IO &&
849 return;
850
852 CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd,
854 nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));
855}
caddr_t cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries, int entry_size, cam_quirkmatch_t *comp_func)
Definition: cam.c:281
void cam_strvis_flag(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen, uint32_t flags)
Definition: cam.c:131
@ SF_NO_RETRY
Definition: cam.h:127
@ SF_NO_RECOVERY
Definition: cam.h:126
#define CAM_TARGET_WILDCARD
Definition: cam.h:48
@ CAM_STRVIS_FLAG_NONASCII_SPC
Definition: cam.h:352
cam_flags
Definition: cam.h:115
@ CAM_EXPECT_INQ_CHANGE
Definition: cam.h:117
#define CAM_LUN_WILDCARD
Definition: cam.h:49
#define CAM_PRIORITY_NORMAL
Definition: cam.h:92
#define CAM_PRIORITY_NONE
Definition: cam.h:93
#define CAM_PRIORITY_XPT
Definition: cam.h:89
cam_status
Definition: cam.h:132
@ CAM_REQ_INVALID
Definition: cam.h:152
@ CAM_REQ_CMP
Definition: cam.h:137
@ CAM_REQ_CMP_ERR
Definition: cam.h:146
@ CAM_STATUS_MASK
Definition: cam.h:302
@ CAM_REQ_ABORTED
Definition: cam.h:140
@ CAM_DEV_QFRZN
Definition: cam.h:287
u_int64_t lun_id_t
Definition: cam.h:44
@ AC_ADVINFO_CHANGED
Definition: cam_ccb.h:868
@ AC_FOUND_DEVICE
Definition: cam_ccb.h:874
@ AC_LOST_DEVICE
Definition: cam_ccb.h:873
#define CDAI_FLAG_STORE
Definition: cam_ccb.h:1312
@ PROTO_NVME
Definition: cam_ccb.h:284
@ CTS_TYPE_CURRENT_SETTINGS
Definition: cam_ccb.h:945
#define CTS_NVME_VALID_LINK
Definition: cam_ccb.h:1045
#define CDAI_TYPE_NVME_CNTRL
Definition: cam_ccb.h:1320
static __inline void cam_fill_nvmeadmin(struct ccb_nvmeio *nvmeio, u_int32_t retries, void(*cbfcnp)(struct cam_periph *, union ccb *), u_int32_t flags, u_int8_t *data_ptr, u_int32_t dxfer_len, u_int32_t timeout)
Definition: cam_ccb.h:1537
@ CAM_DIR_IN
Definition: cam_ccb.h:79
@ CAM_DEV_QFREEZE
Definition: cam_ccb.h:92
@ XPT_DEV_ADVINFO
Definition: cam_ccb.h:166
@ XPT_NVME_ADMIN
Definition: cam_ccb.h:227
@ XPT_GET_TRAN_SETTINGS
Definition: cam_ccb.h:183
@ XPT_SCAN_TGT
Definition: cam_ccb.h:223
@ XPT_SCAN_BUS
Definition: cam_ccb.h:155
@ XPT_SET_TRAN_SETTINGS
Definition: cam_ccb.h:188
@ XPT_NVME_IO
Definition: cam_ccb.h:217
@ XPT_SCAN_LUN
Definition: cam_ccb.h:180
@ XPT_GDEV_TYPE
Definition: cam_ccb.h:143
#define CDAI_TYPE_SERIAL_NUM
Definition: cam_ccb.h:1316
#define CDAI_TYPE_PHYS_PATH
Definition: cam_ccb.h:1317
#define CDAI_TYPE_NVME_NS
Definition: cam_ccb.h:1321
#define CDAI_TYPE_SCSI_DEVID
Definition: cam_ccb.h:1315
@ CAM_DEBUG_TRACE
Definition: cam_debug.h:41
@ CAM_DEBUG_PROBE
Definition: cam_debug.h:46
@ CAM_DEBUG_CDB
Definition: cam_debug.h:43
#define CAM_DEBUG(path, flag, printfargs)
Definition: cam_debug.h:93
void cam_periph_release_locked(struct cam_periph *periph)
Definition: cam_periph.c:453
struct cam_periph * cam_periph_find(struct cam_path *path, char *name)
Definition: cam_periph.c:340
u_int32_t cam_release_devq(struct cam_path *path, u_int32_t relsim_flags, u_int32_t openings, u_int32_t arg, int getcount_only)
Definition: cam_periph.c:1342
int cam_periph_acquire(struct cam_periph *periph)
Definition: cam_periph.c:413
void cam_periph_invalidate(struct cam_periph *periph)
Definition: cam_periph.c:655
int cam_periph_error(union ccb *ccb, cam_flags camflags, u_int32_t sense_flags)
Definition: cam_periph.c:1864
cam_status cam_periph_alloc(periph_ctor_t *periph_ctor, periph_oninv_t *periph_oninvalidate, periph_dtor_t *periph_dtor, periph_start_t *periph_start, char *name, cam_periph_type type, struct cam_path *path, ac_callback_t *ac_callback, ac_code code, void *arg)
Definition: cam_periph.c:197
#define CAM_PERIPH_INVALID
Definition: cam_periph.h:133
#define cam_periph_assert(periph, what)
Definition: cam_periph.h:230
#define CAM_PERIPH_DRV_EARLY
Definition: cam_periph.h:99
@ CAM_PERIPH_BIO
Definition: cam_periph.h:104
void() periph_init_t(void)
Definition: cam_periph.h:85
void xpt_acquire_device(struct cam_ed *device)
Definition: cam_xpt.c:4897
void xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
Definition: cam_xpt.c:3243
void xpt_release_device(struct cam_ed *device)
Definition: cam_xpt.c:4907
void xpt_print(struct cam_path *path, const char *fmt,...)
Definition: cam_xpt.c:3814
void xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
Definition: cam_xpt.c:4350
void xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
Definition: cam_xpt.c:3520
void xpt_action(union ccb *start_ccb)
Definition: cam_xpt.c:2601
void xpt_done(union ccb *done_ccb)
Definition: cam_xpt.c:4562
lun_id_t xpt_path_lun_id(struct cam_path *path)
Definition: cam_xpt.c:3895
void xpt_action_default(union ccb *start_ccb)
Definition: cam_xpt.c:2613
void xpt_release_ccb(union ccb *free_ccb)
Definition: cam_xpt.c:3924
struct cam_ed * xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
Definition: cam_xpt.c:4840
void xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
Definition: cam_xpt.c:4480
#define xpt_path_unlock(path)
Definition: cam_xpt.h:129
#define xpt_path_owned(path)
Definition: cam_xpt.h:131
#define xpt_path_lock(path)
Definition: cam_xpt.h:128
#define xpt_path_assert(path, what)
Definition: cam_xpt.h:130
static void xpt_path_inq(struct ccb_pathinq *cpi, struct cam_path *path)
Definition: cam_xpt.h:156
#define CAM_DEV_UNCONFIGURED
union ccb * ccb
Definition: mmc_sim_if.m:53
struct ccb_trans_settings_mmc * cts
Definition: mmc_sim_if.m:43
const char * nvme_cmd_string(const struct nvme_command *cmd, char *cmd_string, size_t len)
Definition: nvme_all.c:146
void nvme_print_ident(const struct nvme_controller_data *cdata, const struct nvme_namespace_data *data, struct sbuf *sb)
Definition: nvme_all.c:89
const char * nvme_op_string(const struct nvme_command *cmd, int admin)
Definition: nvme_all.c:133
int nvme_identify_match(caddr_t identbuffer, caddr_t table_entry)
Definition: nvme_all.c:83
void nvme_ns_cmd(struct ccb_nvmeio *nvmeio, uint8_t cmd, uint32_t nsid, uint32_t cdw10, uint32_t cdw11, uint32_t cdw12, uint32_t cdw13, uint32_t cdw14, uint32_t cdw15)
Definition: nvme_all.c:67
static struct xpt_xport_ops nvme_xport_ops
Definition: nvme_xpt.c:164
static void nvme_proto_announce(struct cam_ed *device)
Definition: nvme_xpt.c:824
static periph_init_t nvme_probe_periph_init
Definition: nvme_xpt.c:74
static struct xpt_proto_ops nvme_proto_ops
Definition: nvme_xpt.c:182
PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver)
static void nvme_action(union ccb *start_ccb)
Definition: nvme_xpt.c:740
static void nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
Definition: nvme_xpt.c:305
static void nvme_probe_cleanup(struct cam_periph *periph)
Definition: nvme_xpt.c:494
CAM_XPT_PROTO(nvme_proto)
static struct cam_ed * nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
Definition: nvme_xpt.c:593
static void nvme_proto_debug_out(union ccb *ccb)
Definition: nvme_xpt.c:843
static void nvme_dev_advinfo(union ccb *start_ccb)
Definition: nvme_xpt.c:650
static cam_status nvme_probe_register(struct cam_periph *periph, void *arg)
Definition: nvme_xpt.c:200
static void nvme_scan_lun(struct cam_periph *periph, struct cam_path *path, cam_flags flags, union ccb *ccb)
Definition: nvme_xpt.c:526
#define CAM_QUIRK_MAXTAGS
Definition: nvme_xpt.c:68
static char * nvme_probe_action_text[]
Definition: nvme_xpt.c:92
static void nvme_proto_denounce(struct cam_ed *device)
Definition: nvme_xpt.c:836
static void nvme_device_transport(struct cam_path *path)
Definition: nvme_xpt.c:620
__FBSDID("$FreeBSD$")
#define NVME_PROBE_SET_ACTION(softc, newaction)
Definition: nvme_xpt.c:99
static struct nvme_quirk_entry nvme_quirk_table[]
Definition: nvme_xpt.c:125
nvme_probe_action
Definition: nvme_xpt.c:85
@ NVME_PROBE_INVALID
Definition: nvme_xpt.c:89
@ NVME_PROBE_IDENTIFY_NS
Definition: nvme_xpt.c:87
@ NVME_PROBE_DONE
Definition: nvme_xpt.c:88
@ NVME_PROBE_IDENTIFY_CD
Definition: nvme_xpt.c:86
#define NVME_XPT_XPORT(x, X)
Definition: nvme_xpt.c:170
static void nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
Definition: nvme_xpt.c:257
static struct xpt_proto nvme_proto
Definition: nvme_xpt.c:187
static const int nvme_quirk_table_size
Definition: nvme_xpt.c:136
static void nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, struct cam_ed *device, void *async_arg)
Definition: nvme_xpt.c:767
nvme_probe_flags
Definition: nvme_xpt.c:109
@ NVME_PROBE_NO_ANNOUNCE
Definition: nvme_xpt.c:110
static void nvme_probe_schedule(struct cam_periph *nvme_probe_periph)
Definition: nvme_xpt.c:238
static void nvme_announce_periph(struct cam_periph *periph)
Definition: nvme_xpt.c:786
static struct periph_driver nvme_probe_driver
Definition: nvme_xpt.c:76
#define SVPD_ID_CODESET_BINARY
Definition: scsi_all.h:2369
#define T_DIRECT
Definition: scsi_all.h:2170
static __inline void scsi_ulto2b(u_int32_t val, u_int8_t *bytes)
Definition: scsi_all.h:4374
#define SVPD_ID_ASSOC_LUN
Definition: scsi_all.h:2375
#define SID_QUAL_LU_CONNECTED
Definition: scsi_all.h:2208
#define SVPD_ID_TYPE_EUI64
Definition: scsi_all.h:2381
#define SVPD_DEVICE_ID_HDR_LEN
Definition: scsi_all.h:2356
#define SVPD_DEVICE_ID
Definition: scsi_all.h:2354
u_int protocol_version
u_int maxtags
uint8_t physpath_len
u_int8_t queue_flags
u_int8_t * serial_num
u_int mintags
struct cam_et * target
u_int8_t serial_num_len
lun_id_t lun_id
u_int8_t inq_flags
struct nvme_namespace_data * nvme_data
uint8_t * device_id
u_int transport_version
uint8_t * physpath
void * quirk
cam_proto protocol
struct nvme_controller_data * nvme_cdata
uint32_t device_id_len
cam_xport transport
u_int32_t flags
target_id_t target_id
struct cam_ed * device
struct cam_periph * periph
char * periph_name
Definition: cam_periph.h:123
u_int32_t unit_number
Definition: cam_periph.h:127
void * softc
Definition: cam_periph.h:125
struct cam_path * path
Definition: cam_periph.h:124
u_int32_t flags
Definition: cam_periph.h:129
u_int32_t priority
Definition: cam.h:86
uint32_t buftype
Definition: cam_ccb.h:1313
uint8_t * buf
Definition: cam_ccb.h:1326
uint32_t flags
Definition: cam_ccb.h:1310
u_int32_t flags
Definition: cam_ccb.h:368
struct cam_path * path
Definition: cam_ccb.h:364
cam_pinfo pinfo
Definition: cam_ccb.h:349
xpt_opcode func_code
Definition: cam_ccb.h:362
u_int32_t status
Definition: cam_ccb.h:363
struct nvme_command cmd
Definition: cam_ccb.h:855
cam_proto protocol
Definition: cam_ccb.h:680
u_int protocol_version
Definition: cam_ccb.h:681
struct ccb_hdr ccb_h
Definition: cam_ccb.h:661
cam_xport transport
Definition: cam_ccb.h:682
u_int transport_version
Definition: cam_ccb.h:683
cam_flags flags
Definition: cam_ccb.h:1191
struct cam_periph * periph
Definition: nvme_xpt.c:122
nvme_probe_action action
Definition: nvme_xpt.c:118
TAILQ_HEAD(ccb_hdr)
Definition: nvme_xpt.c:114
nvme_probe_flags flags
Definition: nvme_xpt.c:120
Definition: nvme_xpt.c:66
u_int quirks
Definition: nvme_xpt.c:67
u_int maxtags
Definition: nvme_xpt.c:70
u_int mintags
Definition: nvme_xpt.c:69
u_int8_t page_code
Definition: scsi_all.h:2353
u_int8_t length[2]
Definition: scsi_all.h:2358
u_int8_t identifier[]
Definition: scsi_all.h:2395
xpt_proto_announce_func announce
cam_proto proto
xpt_alloc_device_func alloc_device
Definition: cam_ccb.h:1345
struct ccb_dev_advinfo cdai
Definition: cam_ccb.h:1377
struct ccb_hdr ccb_h
Definition: cam_ccb.h:1346
struct ccb_rescan crcn
Definition: cam_ccb.h:1374
struct ccb_nvmeio nvmeio
Definition: cam_ccb.h:1379