xref: /freebsd/sys/cam/nvme/nvme_xpt.c (revision f5ef5f675d9d9eb6e35ed9142d70ecf774456ad4)
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>
57 #include <cam/cam_xpt_internal.h>
58 #include <cam/cam_debug.h>
59 
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 */
64 #include "opt_cam.h"
65 
66 struct nvme_quirk_entry {
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 */
74 static periph_init_t nvme_probe_periph_init;
75 
76 static struct periph_driver nvme_probe_driver =
77 {
78 	nvme_probe_periph_init, "nvme_probe",
79 	TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
80 	CAM_PERIPH_DRV_EARLY
81 };
82 
83 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
84 
85 typedef enum {
86 	NVME_PROBE_IDENTIFY_CD,
87 	NVME_PROBE_IDENTIFY_NS,
88 	NVME_PROBE_DONE,
89 	NVME_PROBE_INVALID
90 } nvme_probe_action;
91 
92 static 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)	\
100 do {									\
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 
109 typedef enum {
110 	NVME_PROBE_NO_ANNOUNCE	= 0x04
111 } nvme_probe_flags;
112 
113 typedef struct {
114 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
115 	union {
116 		struct nvme_controller_data	cd;
117 		struct nvme_namespace_data	ns;
118 	};
119 	nvme_probe_action	action;
120 	nvme_probe_flags	flags;
121 	int		restart;
122 	struct cam_periph *periph;
123 } nvme_probe_softc;
124 
125 static struct nvme_quirk_entry nvme_quirk_table[] =
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 
136 static const int nvme_quirk_table_size =
137 	sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
138 
139 static cam_status	nvme_probe_register(struct cam_periph *periph,
140 				      void *arg);
141 static void	 nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
142 static void	 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
143 static void	 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb);
144 static void	 nvme_probe_cleanup(struct cam_periph *periph);
145 //static void	 nvme_find_quirk(struct cam_ed *device);
146 static void	 nvme_scan_lun(struct cam_periph *periph,
147 			       struct cam_path *path, cam_flags flags,
148 			       union ccb *ccb);
149 static struct cam_ed *
150 		 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
151 				   lun_id_t lun_id);
152 static void	 nvme_device_transport(struct cam_path *path);
153 static 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);
158 static void	 nvme_action(union ccb *start_ccb);
159 static void	 nvme_announce_periph(struct cam_periph *periph);
160 static void	 nvme_proto_announce(struct cam_ed *device);
161 static void	 nvme_proto_denounce(struct cam_ed *device);
162 static void	 nvme_proto_debug_out(union ccb *ccb);
163 
164 static struct xpt_xport_ops nvme_xport_ops = {
165 	.alloc_device = nvme_alloc_device,
166 	.action = nvme_action,
167 	.async = nvme_dev_async,
168 	.announce = nvme_announce_periph,
169 };
170 #define NVME_XPT_XPORT(x, X)			\
171 static struct xpt_xport nvme_xport_ ## x = {	\
172 	.xport = XPORT_ ## X,			\
173 	.name = #x,				\
174 	.ops = &nvme_xport_ops,			\
175 };						\
176 CAM_XPT_XPORT(nvme_xport_ ## x);
177 
178 NVME_XPT_XPORT(nvme, NVME);
179 
180 #undef NVME_XPT_XPORT
181 
182 static struct xpt_proto_ops nvme_proto_ops = {
183 	.announce = nvme_proto_announce,
184 	.denounce = nvme_proto_denounce,
185 	.debug_out = nvme_proto_debug_out,
186 };
187 static struct xpt_proto nvme_proto = {
188 	.proto = PROTO_NVME,
189 	.name = "nvme",
190 	.ops = &nvme_proto_ops,
191 };
192 CAM_XPT_PROTO(nvme_proto);
193 
194 static void
195 nvme_probe_periph_init()
196 {
197 
198 }
199 
200 static cam_status
201 nvme_probe_register(struct cam_periph *periph, void *arg)
202 {
203 	union ccb *request_ccb;	/* CCB representing the probe request */
204 	nvme_probe_softc *softc;
205 
206 	request_ccb = (union ccb *)arg;
207 	if (request_ccb == NULL) {
208 		printf("nvme_probe_register: no probe CCB, "
209 		       "can't register device\n");
210 		return(CAM_REQ_CMP_ERR);
211 	}
212 
213 	softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
214 
215 	if (softc == NULL) {
216 		printf("nvme_probe_register: Unable to probe new device. "
217 		       "Unable to allocate softc\n");
218 		return(CAM_REQ_CMP_ERR);
219 	}
220 	TAILQ_INIT(&softc->request_ccbs);
221 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
222 			  periph_links.tqe);
223 	softc->flags = 0;
224 	periph->softc = softc;
225 	softc->periph = periph;
226 	softc->action = NVME_PROBE_INVALID;
227 	if (cam_periph_acquire(periph) != 0)
228 		return (CAM_REQ_CMP_ERR);
229 
230 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
231 
232 //	nvme_device_transport(periph->path);
233 	nvme_probe_schedule(periph);
234 
235 	return(CAM_REQ_CMP);
236 }
237 
238 static void
239 nvme_probe_schedule(struct cam_periph *periph)
240 {
241 	union ccb *ccb;
242 	nvme_probe_softc *softc;
243 
244 	softc = (nvme_probe_softc *)periph->softc;
245 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
246 
247 	NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
248 
249 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
250 		softc->flags |= NVME_PROBE_NO_ANNOUNCE;
251 	else
252 		softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
253 
254 	xpt_schedule(periph, CAM_PRIORITY_XPT);
255 }
256 
257 static void
258 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
259 {
260 	struct ccb_nvmeio *nvmeio;
261 	nvme_probe_softc *softc;
262 	struct cam_path *path;
263 	lun_id_t lun;
264 
265 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
266 
267 	softc = (nvme_probe_softc *)periph->softc;
268 	path = start_ccb->ccb_h.path;
269 	nvmeio = &start_ccb->nvmeio;
270 	lun = xpt_path_lun_id(periph->path);
271 
272 	if (softc->restart) {
273 		softc->restart = 0;
274 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
275 	}
276 
277 	switch (softc->action) {
278 	case NVME_PROBE_IDENTIFY_CD:
279 		cam_fill_nvmeadmin(nvmeio,
280 		    0,			/* retries */
281 		    nvme_probe_done,	/* cbfcnp */
282 		    CAM_DIR_IN,		/* flags */
283 		    (uint8_t *)&softc->cd,	/* data_ptr */
284 		    sizeof(softc->cd),		/* dxfer_len */
285 		    30 * 1000); /* timeout 30s */
286 		nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0,
287 		    1, 0, 0, 0, 0, 0);
288 		break;
289 	case NVME_PROBE_IDENTIFY_NS:
290 		cam_fill_nvmeadmin(nvmeio,
291 		    0,			/* retries */
292 		    nvme_probe_done,	/* cbfcnp */
293 		    CAM_DIR_IN,		/* flags */
294 		    (uint8_t *)&softc->ns,	/* data_ptr */
295 		    sizeof(softc->ns),		/* dxfer_len */
296 		    30 * 1000); /* timeout 30s */
297 		nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun,
298 		    0, 0, 0, 0, 0, 0);
299 		break;
300 	default:
301 		panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
302 	}
303 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
304 	xpt_action(start_ccb);
305 }
306 
307 static void
308 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
309 {
310 	struct nvme_namespace_data *nvme_data;
311 	struct nvme_controller_data *nvme_cdata;
312 	nvme_probe_softc *softc;
313 	struct cam_path *path;
314 	cam_status status;
315 	u_int32_t  priority;
316 	int found = 1;
317 
318 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n"));
319 
320 	softc = (nvme_probe_softc *)periph->softc;
321 	path = done_ccb->ccb_h.path;
322 	priority = done_ccb->ccb_h.pinfo.priority;
323 
324 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
325 		if (cam_periph_error(done_ccb,
326 			0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
327 		    ) == ERESTART) {
328 out:
329 			/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
330 			cam_release_devq(path, 0, 0, 0, FALSE);
331 			return;
332 		}
333 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
334 			/* Don't wedge the queue */
335 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
336 		}
337 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
338 
339 		/*
340 		 * If we get to this point, we got an error status back
341 		 * from the inquiry and the error status doesn't require
342 		 * automatically retrying the command.  Therefore, the
343 		 * inquiry failed.  If we had inquiry information before
344 		 * for this device, but this latest inquiry command failed,
345 		 * the device has probably gone away.  If this device isn't
346 		 * already marked unconfigured, notify the peripheral
347 		 * drivers that this device is no more.
348 		 */
349 device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
350 			xpt_async(AC_LOST_DEVICE, path, NULL);
351 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID);
352 		found = 0;
353 		goto done;
354 	}
355 	if (softc->restart)
356 		goto done;
357 	switch (softc->action) {
358 	case NVME_PROBE_IDENTIFY_CD:
359 		nvme_controller_data_swapbytes(&softc->cd);
360 
361 		nvme_cdata = path->device->nvme_cdata;
362 		if (nvme_cdata == NULL) {
363 			nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT,
364 			    M_NOWAIT);
365 			if (nvme_cdata == NULL) {
366 				xpt_print(path, "Can't allocate memory");
367 				goto device_fail;
368 			}
369 		}
370 		bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata));
371 		path->device->nvme_cdata = nvme_cdata;
372 
373 //		nvme_find_quirk(path->device);
374 		nvme_device_transport(path);
375 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS);
376 		xpt_release_ccb(done_ccb);
377 		xpt_schedule(periph, priority);
378 		goto out;
379 	case NVME_PROBE_IDENTIFY_NS:
380 		nvme_namespace_data_swapbytes(&softc->ns);
381 
382 		/* Check that the namespace exists. */
383 		if (softc->ns.nsze == 0)
384 			goto device_fail;
385 
386 		nvme_data = path->device->nvme_data;
387 		if (nvme_data == NULL) {
388 			nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
389 			    M_NOWAIT);
390 			if (nvme_data == NULL) {
391 				xpt_print(path, "Can't allocate memory");
392 				goto device_fail;
393 			}
394 		}
395 		bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
396 		path->device->nvme_data = nvme_data;
397 
398 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
399 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
400 			xpt_acquire_device(path->device);
401 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
402 			xpt_action(done_ccb);
403 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
404 		}
405 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
406 		break;
407 	default:
408 		panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
409 	}
410 done:
411 	if (softc->restart) {
412 		softc->restart = 0;
413 		xpt_release_ccb(done_ccb);
414 		nvme_probe_schedule(periph);
415 		goto out;
416 	}
417 	xpt_release_ccb(done_ccb);
418 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
419 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
420 		TAILQ_REMOVE(&softc->request_ccbs,
421 		    &done_ccb->ccb_h, periph_links.tqe);
422 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
423 		xpt_done(done_ccb);
424 	}
425 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
426 	cam_release_devq(path, 0, 0, 0, FALSE);
427 	cam_periph_invalidate(periph);
428 	cam_periph_release_locked(periph);
429 }
430 
431 static void
432 nvme_probe_cleanup(struct cam_periph *periph)
433 {
434 
435 	free(periph->softc, M_CAMXPT);
436 }
437 
438 #if 0
439 /* XXX should be used, don't delete */
440 static void
441 nvme_find_quirk(struct cam_ed *device)
442 {
443 	struct nvme_quirk_entry *quirk;
444 	caddr_t	match;
445 
446 	match = cam_quirkmatch((caddr_t)&device->nvme_data,
447 			       (caddr_t)nvme_quirk_table,
448 			       nvme_quirk_table_size,
449 			       sizeof(*nvme_quirk_table), nvme_identify_match);
450 
451 	if (match == NULL)
452 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
453 
454 	quirk = (struct nvme_quirk_entry *)match;
455 	device->quirk = quirk;
456 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
457 		device->mintags = quirk->mintags;
458 		device->maxtags = quirk->maxtags;
459 	}
460 }
461 #endif
462 
463 static void
464 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
465 	     cam_flags flags, union ccb *request_ccb)
466 {
467 	struct ccb_pathinq cpi;
468 	cam_status status;
469 	struct cam_periph *old_periph;
470 	int lock;
471 
472 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
473 
474 	xpt_path_inq(&cpi, path);
475 
476 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
477 		if (request_ccb != NULL) {
478 			request_ccb->ccb_h.status = cpi.ccb_h.status;
479 			xpt_done(request_ccb);
480 		}
481 		return;
482 	}
483 
484 	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
485 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
486 		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
487 		xpt_done(request_ccb);
488 		return;
489 	}
490 
491 	lock = (xpt_path_owned(path) == 0);
492 	if (lock)
493 		xpt_path_lock(path);
494 	if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
495 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
496 			nvme_probe_softc *softc;
497 
498 			softc = (nvme_probe_softc *)old_periph->softc;
499 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
500 				&request_ccb->ccb_h, periph_links.tqe);
501 			softc->restart = 1;
502 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
503 			    ("restarting nvme_probe device\n"));
504 		} else {
505 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
506 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
507 			    ("Failing to restart nvme_probe device\n"));
508 			xpt_done(request_ccb);
509 		}
510 	} else {
511 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
512 		    ("Adding nvme_probe device\n"));
513 		status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
514 					  nvme_probe_start, "nvme_probe",
515 					  CAM_PERIPH_BIO,
516 					  request_ccb->ccb_h.path, NULL, 0,
517 					  request_ccb);
518 
519 		if (status != CAM_REQ_CMP) {
520 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
521 			    "returned an error, can't continue probe\n");
522 			request_ccb->ccb_h.status = status;
523 			xpt_done(request_ccb);
524 		}
525 	}
526 	if (lock)
527 		xpt_path_unlock(path);
528 }
529 
530 static struct cam_ed *
531 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
532 {
533 	struct nvme_quirk_entry *quirk;
534 	struct cam_ed *device;
535 
536 	device = xpt_alloc_device(bus, target, lun_id);
537 	if (device == NULL)
538 		return (NULL);
539 
540 	/*
541 	 * Take the default quirk entry until we have inquiry
542 	 * data from nvme and can determine a better quirk to use.
543 	 */
544 	quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
545 	device->quirk = (void *)quirk;
546 	device->mintags = 0;
547 	device->maxtags = 0;
548 	device->inq_flags = 0;
549 	device->queue_flags = 0;
550 	device->device_id = NULL;	/* XXX Need to set this somewhere */
551 	device->device_id_len = 0;
552 	device->serial_num = NULL;	/* XXX Need to set this somewhere */
553 	device->serial_num_len = 0;
554 	return (device);
555 }
556 
557 static void
558 nvme_device_transport(struct cam_path *path)
559 {
560 	struct ccb_pathinq cpi;
561 	struct ccb_trans_settings cts;
562 	/* XXX get data from nvme namespace and other info ??? */
563 
564 	/* Get transport information from the SIM */
565 	xpt_path_inq(&cpi, path);
566 
567 	path->device->transport = cpi.transport;
568 	path->device->transport_version = cpi.transport_version;
569 
570 	path->device->protocol = cpi.protocol;
571 	path->device->protocol_version = cpi.protocol_version;
572 
573 	/* Tell the controller what we think */
574 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
575 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
576 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
577 	cts.transport = path->device->transport;
578 	cts.transport_version = path->device->transport_version;
579 	cts.protocol = path->device->protocol;
580 	cts.protocol_version = path->device->protocol_version;
581 	cts.proto_specific.valid = 0;
582 	cts.xport_specific.valid = 0;
583 	xpt_action((union ccb *)&cts);
584 }
585 
586 static void
587 nvme_dev_advinfo(union ccb *start_ccb)
588 {
589 	struct cam_ed *device;
590 	struct ccb_dev_advinfo *cdai;
591 	off_t amt;
592 
593 	xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
594 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
595 	device = start_ccb->ccb_h.path->device;
596 	cdai = &start_ccb->cdai;
597 	switch(cdai->buftype) {
598 	case CDAI_TYPE_SCSI_DEVID:
599 		if (cdai->flags & CDAI_FLAG_STORE)
600 			return;
601 		cdai->provsiz = device->device_id_len;
602 		if (device->device_id_len == 0)
603 			break;
604 		amt = device->device_id_len;
605 		if (cdai->provsiz > cdai->bufsiz)
606 			amt = cdai->bufsiz;
607 		memcpy(cdai->buf, device->device_id, amt);
608 		break;
609 	case CDAI_TYPE_SERIAL_NUM:
610 		if (cdai->flags & CDAI_FLAG_STORE)
611 			return;
612 		cdai->provsiz = device->serial_num_len;
613 		if (device->serial_num_len == 0)
614 			break;
615 		amt = device->serial_num_len;
616 		if (cdai->provsiz > cdai->bufsiz)
617 			amt = cdai->bufsiz;
618 		memcpy(cdai->buf, device->serial_num, amt);
619 		break;
620 	case CDAI_TYPE_PHYS_PATH:
621 		if (cdai->flags & CDAI_FLAG_STORE) {
622 			if (device->physpath != NULL)
623 				free(device->physpath, M_CAMXPT);
624 			device->physpath_len = cdai->bufsiz;
625 			/* Clear existing buffer if zero length */
626 			if (cdai->bufsiz == 0)
627 				break;
628 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
629 			if (device->physpath == NULL) {
630 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
631 				return;
632 			}
633 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
634 		} else {
635 			cdai->provsiz = device->physpath_len;
636 			if (device->physpath_len == 0)
637 				break;
638 			amt = device->physpath_len;
639 			if (cdai->provsiz > cdai->bufsiz)
640 				amt = cdai->bufsiz;
641 			memcpy(cdai->buf, device->physpath, amt);
642 		}
643 		break;
644 	case CDAI_TYPE_NVME_CNTRL:
645 		if (cdai->flags & CDAI_FLAG_STORE)
646 			return;
647 		amt = sizeof(struct nvme_controller_data);
648 		cdai->provsiz = amt;
649 		if (amt > cdai->bufsiz)
650 			amt = cdai->bufsiz;
651 		memcpy(cdai->buf, device->nvme_cdata, amt);
652 		break;
653 	case CDAI_TYPE_NVME_NS:
654 		if (cdai->flags & CDAI_FLAG_STORE)
655 			return;
656 		amt = sizeof(struct nvme_namespace_data);
657 		cdai->provsiz = amt;
658 		if (amt > cdai->bufsiz)
659 			amt = cdai->bufsiz;
660 		memcpy(cdai->buf, device->nvme_data, amt);
661 		break;
662 	default:
663 		return;
664 	}
665 	start_ccb->ccb_h.status = CAM_REQ_CMP;
666 
667 	if (cdai->flags & CDAI_FLAG_STORE) {
668 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
669 			  (void *)(uintptr_t)cdai->buftype);
670 	}
671 }
672 
673 static void
674 nvme_action(union ccb *start_ccb)
675 {
676 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
677 	    ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
678 
679 	switch (start_ccb->ccb_h.func_code) {
680 	case XPT_SCAN_BUS:
681 	case XPT_SCAN_TGT:
682 	case XPT_SCAN_LUN:
683 		nvme_scan_lun(start_ccb->ccb_h.path->periph,
684 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
685 			      start_ccb);
686 		break;
687 	case XPT_DEV_ADVINFO:
688 		nvme_dev_advinfo(start_ccb);
689 		break;
690 
691 	default:
692 		xpt_action_default(start_ccb);
693 		break;
694 	}
695 }
696 
697 /*
698  * Handle any per-device event notifications that require action by the XPT.
699  */
700 static void
701 nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
702 	      struct cam_ed *device, void *async_arg)
703 {
704 
705 	/*
706 	 * We only need to handle events for real devices.
707 	 */
708 	if (target->target_id == CAM_TARGET_WILDCARD
709 	 || device->lun_id == CAM_LUN_WILDCARD)
710 		return;
711 
712 	if (async_code == AC_LOST_DEVICE &&
713 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
714 		device->flags |= CAM_DEV_UNCONFIGURED;
715 		xpt_release_device(device);
716 	}
717 }
718 
719 static void
720 nvme_announce_periph(struct cam_periph *periph)
721 {
722 	struct	ccb_pathinq cpi;
723 	struct	ccb_trans_settings cts;
724 	struct	cam_path *path = periph->path;
725 	struct ccb_trans_settings_nvme	*nvmex;
726 	struct sbuf	sb;
727 	char		buffer[120];
728 
729 	cam_periph_assert(periph, MA_OWNED);
730 
731 	/* Ask the SIM for connection details */
732 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
733 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
734 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
735 	xpt_action((union ccb*)&cts);
736 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
737 		return;
738 	nvmex = &cts.xport_specific.nvme;
739 
740 	/* Ask the SIM for its base transfer speed */
741 	xpt_path_inq(&cpi, periph->path);
742 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
743 	sbuf_printf(&sb, "%s%d: nvme version %d.%d",
744 	    periph->periph_name, periph->unit_number,
745 	    NVME_MAJOR(nvmex->spec),
746 	    NVME_MINOR(nvmex->spec));
747 	if (nvmex->valid & CTS_NVME_VALID_LINK)
748 		sbuf_printf(&sb, " x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
749 		    nvmex->lanes, nvmex->max_lanes,
750 		    nvmex->speed, nvmex->max_speed);
751 	sbuf_printf(&sb, "\n");
752 	sbuf_finish(&sb);
753 	sbuf_putbuf(&sb);
754 }
755 
756 static void
757 nvme_proto_announce(struct cam_ed *device)
758 {
759 	struct sbuf	sb;
760 	char		buffer[120];
761 
762 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
763 	nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
764 	sbuf_finish(&sb);
765 	sbuf_putbuf(&sb);
766 }
767 
768 static void
769 nvme_proto_denounce(struct cam_ed *device)
770 {
771 
772 	nvme_proto_announce(device);
773 }
774 
775 static void
776 nvme_proto_debug_out(union ccb *ccb)
777 {
778 	char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
779 
780 	if (ccb->ccb_h.func_code != XPT_NVME_IO &&
781 	    ccb->ccb_h.func_code != XPT_NVME_ADMIN)
782 		return;
783 
784 	CAM_DEBUG(ccb->ccb_h.path,
785 	    CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd,
786 		ccb->ccb_h.func_code == XPT_NVME_ADMIN),
787 		nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));
788 }
789 
790