xref: /freebsd/sys/cam/nvme/nvme_xpt.c (revision ab40f58ccfe6c07ebefddc72f4661a52fe746353)
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/interrupt.h>
44 #include <sys/sbuf.h>
45 
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 
50 #include <cam/cam.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>
60 
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 */
65 #include "opt_cam.h"
66 
67 struct nvme_quirk_entry {
68 	u_int quirks;
69 #define CAM_QUIRK_MAXTAGS 1
70 	u_int mintags;
71 	u_int maxtags;
72 };
73 
74 /* Not even sure why we need this */
75 static periph_init_t nvme_probe_periph_init;
76 
77 static struct periph_driver nvme_probe_driver =
78 {
79 	nvme_probe_periph_init, "nvme_probe",
80 	TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
81 	CAM_PERIPH_DRV_EARLY
82 };
83 
84 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
85 
86 typedef enum {
87 	NVME_PROBE_IDENTIFY_CD,
88 	NVME_PROBE_IDENTIFY_NS,
89 	NVME_PROBE_DONE,
90 	NVME_PROBE_INVALID
91 } nvme_probe_action;
92 
93 static char *nvme_probe_action_text[] = {
94 	"NVME_PROBE_IDENTIFY_CD",
95 	"NVME_PROBE_IDENTIFY_NS",
96 	"NVME_PROBE_DONE",
97 	"NVME_PROBE_INVALID"
98 };
99 
100 #define NVME_PROBE_SET_ACTION(softc, newaction)	\
101 do {									\
102 	char **text;							\
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);					\
108 } while(0)
109 
110 typedef enum {
111 	NVME_PROBE_NO_ANNOUNCE	= 0x04
112 } nvme_probe_flags;
113 
114 typedef struct {
115 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
116 	union {
117 		struct nvme_controller_data	cd;
118 		struct nvme_namespace_data	ns;
119 	};
120 	nvme_probe_action	action;
121 	nvme_probe_flags	flags;
122 	int		restart;
123 	struct cam_periph *periph;
124 } nvme_probe_softc;
125 
126 static struct nvme_quirk_entry nvme_quirk_table[] =
127 {
128 	{
129 //		{
130 //		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
131 //		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
132 //		},
133 		.quirks = 0, .mintags = 0, .maxtags = 0
134 	},
135 };
136 
137 static const int nvme_quirk_table_size =
138 	sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
139 
140 static cam_status	nvme_probe_register(struct cam_periph *periph,
141 				      void *arg);
142 static void	 nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
143 static void	 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
144 static void	 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb);
145 static void	 nvme_probe_cleanup(struct cam_periph *periph);
146 //static void	 nvme_find_quirk(struct cam_ed *device);
147 static void	 nvme_scan_lun(struct cam_periph *periph,
148 			       struct cam_path *path, cam_flags flags,
149 			       union ccb *ccb);
150 static struct cam_ed *
151 		 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
152 				   lun_id_t lun_id);
153 static void	 nvme_device_transport(struct cam_path *path);
154 static void	 nvme_dev_async(u_int32_t async_code,
155 				struct cam_eb *bus,
156 				struct cam_et *target,
157 				struct cam_ed *device,
158 				void *async_arg);
159 static void	 nvme_action(union ccb *start_ccb);
160 static void	 nvme_announce_periph(struct cam_periph *periph);
161 static void	 nvme_proto_announce(struct cam_ed *device);
162 static void	 nvme_proto_denounce(struct cam_ed *device);
163 static void	 nvme_proto_debug_out(union ccb *ccb);
164 
165 static struct xpt_xport_ops nvme_xport_ops = {
166 	.alloc_device = nvme_alloc_device,
167 	.action = nvme_action,
168 	.async = nvme_dev_async,
169 	.announce = nvme_announce_periph,
170 };
171 #define NVME_XPT_XPORT(x, X)			\
172 static struct xpt_xport nvme_xport_ ## x = {	\
173 	.xport = XPORT_ ## X,			\
174 	.name = #x,				\
175 	.ops = &nvme_xport_ops,			\
176 };						\
177 CAM_XPT_XPORT(nvme_xport_ ## x);
178 
179 NVME_XPT_XPORT(nvme, NVME);
180 
181 #undef NVME_XPT_XPORT
182 
183 static struct xpt_proto_ops nvme_proto_ops = {
184 	.announce = nvme_proto_announce,
185 	.denounce = nvme_proto_denounce,
186 	.debug_out = nvme_proto_debug_out,
187 };
188 static struct xpt_proto nvme_proto = {
189 	.proto = PROTO_NVME,
190 	.name = "nvme",
191 	.ops = &nvme_proto_ops,
192 };
193 CAM_XPT_PROTO(nvme_proto);
194 
195 static void
196 nvme_probe_periph_init()
197 {
198 
199 }
200 
201 static cam_status
202 nvme_probe_register(struct cam_periph *periph, void *arg)
203 {
204 	union ccb *request_ccb;	/* CCB representing the probe request */
205 	nvme_probe_softc *softc;
206 
207 	request_ccb = (union ccb *)arg;
208 	if (request_ccb == NULL) {
209 		printf("nvme_probe_register: no probe CCB, "
210 		       "can't register device\n");
211 		return(CAM_REQ_CMP_ERR);
212 	}
213 
214 	softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
215 
216 	if (softc == NULL) {
217 		printf("nvme_probe_register: Unable to probe new device. "
218 		       "Unable to allocate softc\n");
219 		return(CAM_REQ_CMP_ERR);
220 	}
221 	TAILQ_INIT(&softc->request_ccbs);
222 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
223 			  periph_links.tqe);
224 	softc->flags = 0;
225 	periph->softc = softc;
226 	softc->periph = periph;
227 	softc->action = NVME_PROBE_INVALID;
228 	if (cam_periph_acquire(periph) != 0)
229 		return (CAM_REQ_CMP_ERR);
230 
231 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
232 
233 //	nvme_device_transport(periph->path);
234 	nvme_probe_schedule(periph);
235 
236 	return(CAM_REQ_CMP);
237 }
238 
239 static void
240 nvme_probe_schedule(struct cam_periph *periph)
241 {
242 	union ccb *ccb;
243 	nvme_probe_softc *softc;
244 
245 	softc = (nvme_probe_softc *)periph->softc;
246 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
247 
248 	NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
249 
250 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
251 		softc->flags |= NVME_PROBE_NO_ANNOUNCE;
252 	else
253 		softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
254 
255 	xpt_schedule(periph, CAM_PRIORITY_XPT);
256 }
257 
258 static void
259 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
260 {
261 	struct ccb_nvmeio *nvmeio;
262 	nvme_probe_softc *softc;
263 	struct cam_path *path;
264 	lun_id_t lun;
265 
266 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
267 
268 	softc = (nvme_probe_softc *)periph->softc;
269 	path = start_ccb->ccb_h.path;
270 	nvmeio = &start_ccb->nvmeio;
271 	lun = xpt_path_lun_id(periph->path);
272 
273 	if (softc->restart) {
274 		softc->restart = 0;
275 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
276 	}
277 
278 	switch (softc->action) {
279 	case NVME_PROBE_IDENTIFY_CD:
280 		cam_fill_nvmeadmin(nvmeio,
281 		    0,			/* retries */
282 		    nvme_probe_done,	/* cbfcnp */
283 		    CAM_DIR_IN,		/* flags */
284 		    (uint8_t *)&softc->cd,	/* data_ptr */
285 		    sizeof(softc->cd),		/* dxfer_len */
286 		    30 * 1000); /* timeout 30s */
287 		nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0,
288 		    1, 0, 0, 0, 0, 0);
289 		break;
290 	case NVME_PROBE_IDENTIFY_NS:
291 		cam_fill_nvmeadmin(nvmeio,
292 		    0,			/* retries */
293 		    nvme_probe_done,	/* cbfcnp */
294 		    CAM_DIR_IN,		/* flags */
295 		    (uint8_t *)&softc->ns,	/* data_ptr */
296 		    sizeof(softc->ns),		/* dxfer_len */
297 		    30 * 1000); /* timeout 30s */
298 		nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun,
299 		    0, 0, 0, 0, 0, 0);
300 		break;
301 	default:
302 		panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
303 	}
304 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
305 	xpt_action(start_ccb);
306 }
307 
308 static void
309 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
310 {
311 	struct nvme_namespace_data *nvme_data;
312 	struct nvme_controller_data *nvme_cdata;
313 	nvme_probe_softc *softc;
314 	struct cam_path *path;
315 	cam_status status;
316 	u_int32_t  priority;
317 	int found = 1;
318 
319 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n"));
320 
321 	softc = (nvme_probe_softc *)periph->softc;
322 	path = done_ccb->ccb_h.path;
323 	priority = done_ccb->ccb_h.pinfo.priority;
324 
325 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
326 		if (cam_periph_error(done_ccb,
327 			0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
328 		    ) == ERESTART) {
329 out:
330 			/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
331 			cam_release_devq(path, 0, 0, 0, FALSE);
332 			return;
333 		}
334 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
335 			/* Don't wedge the queue */
336 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
337 		}
338 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
339 
340 		/*
341 		 * If we get to this point, we got an error status back
342 		 * from the inquiry and the error status doesn't require
343 		 * automatically retrying the command.  Therefore, the
344 		 * inquiry failed.  If we had inquiry information before
345 		 * for this device, but this latest inquiry command failed,
346 		 * the device has probably gone away.  If this device isn't
347 		 * already marked unconfigured, notify the peripheral
348 		 * drivers that this device is no more.
349 		 */
350 device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
351 			xpt_async(AC_LOST_DEVICE, path, NULL);
352 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID);
353 		found = 0;
354 		goto done;
355 	}
356 	if (softc->restart)
357 		goto done;
358 	switch (softc->action) {
359 	case NVME_PROBE_IDENTIFY_CD:
360 		nvme_controller_data_swapbytes(&softc->cd);
361 
362 		nvme_cdata = path->device->nvme_cdata;
363 		if (nvme_cdata == NULL) {
364 			nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT,
365 			    M_NOWAIT);
366 			if (nvme_cdata == NULL) {
367 				xpt_print(path, "Can't allocate memory");
368 				goto device_fail;
369 			}
370 		}
371 		bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata));
372 		path->device->nvme_cdata = nvme_cdata;
373 
374 //		nvme_find_quirk(path->device);
375 		nvme_device_transport(path);
376 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS);
377 		xpt_release_ccb(done_ccb);
378 		xpt_schedule(periph, priority);
379 		goto out;
380 	case NVME_PROBE_IDENTIFY_NS:
381 		nvme_namespace_data_swapbytes(&softc->ns);
382 
383 		/* Check that the namespace exists. */
384 		if (softc->ns.nsze == 0)
385 			goto device_fail;
386 
387 		nvme_data = path->device->nvme_data;
388 		if (nvme_data == NULL) {
389 			nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
390 			    M_NOWAIT);
391 			if (nvme_data == NULL) {
392 				xpt_print(path, "Can't allocate memory");
393 				goto device_fail;
394 			}
395 		}
396 		bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
397 		path->device->nvme_data = nvme_data;
398 
399 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
400 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
401 			xpt_acquire_device(path->device);
402 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
403 			xpt_action(done_ccb);
404 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
405 		}
406 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
407 		break;
408 	default:
409 		panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
410 	}
411 done:
412 	if (softc->restart) {
413 		softc->restart = 0;
414 		xpt_release_ccb(done_ccb);
415 		nvme_probe_schedule(periph);
416 		goto out;
417 	}
418 	xpt_release_ccb(done_ccb);
419 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
420 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
421 		TAILQ_REMOVE(&softc->request_ccbs,
422 		    &done_ccb->ccb_h, periph_links.tqe);
423 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
424 		xpt_done(done_ccb);
425 	}
426 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
427 	cam_release_devq(path, 0, 0, 0, FALSE);
428 	cam_periph_invalidate(periph);
429 	cam_periph_release_locked(periph);
430 }
431 
432 static void
433 nvme_probe_cleanup(struct cam_periph *periph)
434 {
435 
436 	free(periph->softc, M_CAMXPT);
437 }
438 
439 #if 0
440 /* XXX should be used, don't delete */
441 static void
442 nvme_find_quirk(struct cam_ed *device)
443 {
444 	struct nvme_quirk_entry *quirk;
445 	caddr_t	match;
446 
447 	match = cam_quirkmatch((caddr_t)&device->nvme_data,
448 			       (caddr_t)nvme_quirk_table,
449 			       nvme_quirk_table_size,
450 			       sizeof(*nvme_quirk_table), nvme_identify_match);
451 
452 	if (match == NULL)
453 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
454 
455 	quirk = (struct nvme_quirk_entry *)match;
456 	device->quirk = quirk;
457 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
458 		device->mintags = quirk->mintags;
459 		device->maxtags = quirk->maxtags;
460 	}
461 }
462 #endif
463 
464 static void
465 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
466 	     cam_flags flags, union ccb *request_ccb)
467 {
468 	struct ccb_pathinq cpi;
469 	cam_status status;
470 	struct cam_periph *old_periph;
471 	int lock;
472 
473 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
474 
475 	xpt_path_inq(&cpi, path);
476 
477 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
478 		if (request_ccb != NULL) {
479 			request_ccb->ccb_h.status = cpi.ccb_h.status;
480 			xpt_done(request_ccb);
481 		}
482 		return;
483 	}
484 
485 	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
486 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
487 		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
488 		xpt_done(request_ccb);
489 		return;
490 	}
491 
492 	lock = (xpt_path_owned(path) == 0);
493 	if (lock)
494 		xpt_path_lock(path);
495 	if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
496 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
497 			nvme_probe_softc *softc;
498 
499 			softc = (nvme_probe_softc *)old_periph->softc;
500 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
501 				&request_ccb->ccb_h, periph_links.tqe);
502 			softc->restart = 1;
503 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
504 			    ("restarting nvme_probe device\n"));
505 		} else {
506 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
507 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
508 			    ("Failing to restart nvme_probe device\n"));
509 			xpt_done(request_ccb);
510 		}
511 	} else {
512 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
513 		    ("Adding nvme_probe device\n"));
514 		status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
515 					  nvme_probe_start, "nvme_probe",
516 					  CAM_PERIPH_BIO,
517 					  request_ccb->ccb_h.path, NULL, 0,
518 					  request_ccb);
519 
520 		if (status != CAM_REQ_CMP) {
521 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
522 			    "returned an error, can't continue probe\n");
523 			request_ccb->ccb_h.status = status;
524 			xpt_done(request_ccb);
525 		}
526 	}
527 	if (lock)
528 		xpt_path_unlock(path);
529 }
530 
531 static struct cam_ed *
532 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
533 {
534 	struct nvme_quirk_entry *quirk;
535 	struct cam_ed *device;
536 
537 	device = xpt_alloc_device(bus, target, lun_id);
538 	if (device == NULL)
539 		return (NULL);
540 
541 	/*
542 	 * Take the default quirk entry until we have inquiry
543 	 * data from nvme and can determine a better quirk to use.
544 	 */
545 	quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
546 	device->quirk = (void *)quirk;
547 	device->mintags = 0;
548 	device->maxtags = 0;
549 	device->inq_flags = 0;
550 	device->queue_flags = 0;
551 	device->device_id = NULL;	/* XXX Need to set this somewhere */
552 	device->device_id_len = 0;
553 	device->serial_num = NULL;	/* XXX Need to set this somewhere */
554 	device->serial_num_len = 0;
555 	return (device);
556 }
557 
558 static void
559 nvme_device_transport(struct cam_path *path)
560 {
561 	struct ccb_pathinq cpi;
562 	struct ccb_trans_settings cts;
563 	/* XXX get data from nvme namespace and other info ??? */
564 
565 	/* Get transport information from the SIM */
566 	xpt_path_inq(&cpi, path);
567 
568 	path->device->transport = cpi.transport;
569 	path->device->transport_version = cpi.transport_version;
570 
571 	path->device->protocol = cpi.protocol;
572 	path->device->protocol_version = cpi.protocol_version;
573 
574 	/* Tell the controller what we think */
575 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
576 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
577 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
578 	cts.transport = path->device->transport;
579 	cts.transport_version = path->device->transport_version;
580 	cts.protocol = path->device->protocol;
581 	cts.protocol_version = path->device->protocol_version;
582 	cts.proto_specific.valid = 0;
583 	cts.xport_specific.valid = 0;
584 	xpt_action((union ccb *)&cts);
585 }
586 
587 static void
588 nvme_dev_advinfo(union ccb *start_ccb)
589 {
590 	struct cam_ed *device;
591 	struct ccb_dev_advinfo *cdai;
592 	off_t amt;
593 
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 
727 	cam_periph_assert(periph, MA_OWNED);
728 
729 	/* Ask the SIM for connection details */
730 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
731 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
732 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
733 	xpt_action((union ccb*)&cts);
734 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
735 		return;
736 	nvmex = &cts.xport_specific.nvme;
737 
738 	/* Ask the SIM for its base transfer speed */
739 	xpt_path_inq(&cpi, periph->path);
740 	printf("%s%d: nvme version %d.%d x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
741 	    periph->periph_name, periph->unit_number,
742 	    NVME_MAJOR(nvmex->spec),
743 	    NVME_MINOR(nvmex->spec),
744 	    nvmex->lanes, nvmex->max_lanes,
745 	    nvmex->speed, nvmex->max_speed);
746 	printf("\n");
747 }
748 
749 static void
750 nvme_proto_announce(struct cam_ed *device)
751 {
752 	struct sbuf	sb;
753 	char		buffer[120];
754 
755 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
756 	nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
757 	sbuf_finish(&sb);
758 	sbuf_putbuf(&sb);
759 }
760 
761 static void
762 nvme_proto_denounce(struct cam_ed *device)
763 {
764 
765 	nvme_proto_announce(device);
766 }
767 
768 static void
769 nvme_proto_debug_out(union ccb *ccb)
770 {
771 	char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
772 
773 	if (ccb->ccb_h.func_code != XPT_NVME_IO)
774 		return;
775 
776 	CAM_DEBUG(ccb->ccb_h.path,
777 	    CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd),
778 		nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));
779 }
780 
781