xref: /freebsd/sys/cam/nvme/nvme_xpt.c (revision 4731124cace5e7a0224e29784617d2856e5c59ab)
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(void)
196 {
197 }
198 
199 static cam_status
200 nvme_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 
237 static void
238 nvme_probe_schedule(struct cam_periph *periph)
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 
246 	NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
247 
248 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
249 		softc->flags |= NVME_PROBE_NO_ANNOUNCE;
250 	else
251 		softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
252 
253 	xpt_schedule(periph, CAM_PRIORITY_XPT);
254 }
255 
256 static void
257 nvme_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;
271 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
272 	}
273 
274 	switch (softc->action) {
275 	case NVME_PROBE_IDENTIFY_CD:
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;
286 	case NVME_PROBE_IDENTIFY_NS:
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 
304 static void
305 nvme_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) {
326 out:
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 		 */
346 device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
347 			xpt_async(AC_LOST_DEVICE, path, NULL);
348 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID);
349 		found = 0;
350 		goto done;
351 	}
352 	if (softc->restart)
353 		goto done;
354 	switch (softc->action) {
355 	case NVME_PROBE_IDENTIFY_CD:
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) {
379 			cam_strvis(path->device->serial_num, nvme_cdata->sn,
380 			    NVME_SERIAL_NUMBER_LENGTH, NVME_SERIAL_NUMBER_LENGTH + 1);
381 			path->device->serial_num_len =
382 			    strlen(path->device->serial_num);
383 		}
384 
385 //		nvme_find_quirk(path->device);
386 		nvme_device_transport(path);
387 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS);
388 		xpt_release_ccb(done_ccb);
389 		xpt_schedule(periph, priority);
390 		goto out;
391 	case NVME_PROBE_IDENTIFY_NS:
392 		nvme_namespace_data_swapbytes(&softc->ns);
393 
394 		/* Check that the namespace exists. */
395 		if (softc->ns.nsze == 0)
396 			goto device_fail;
397 
398 		nvme_data = path->device->nvme_data;
399 		if (nvme_data == NULL) {
400 			nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
401 			    M_NOWAIT);
402 			if (nvme_data == NULL) {
403 				xpt_print(path, "Can't allocate memory");
404 				goto device_fail;
405 			}
406 		}
407 		bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
408 		path->device->nvme_data = nvme_data;
409 
410 		/* Save/update device_id based on NGUID and/or EUI64. */
411 		if (path->device->device_id != NULL) {
412 			free(path->device->device_id, M_CAMXPT);
413 			path->device->device_id = NULL;
414 			path->device->device_id_len = 0;
415 		}
416 		len = 0;
417 		for (g = 0; g < sizeof(nvme_data->nguid); g++) {
418 			if (nvme_data->nguid[g] != 0)
419 				break;
420 		}
421 		if (g < sizeof(nvme_data->nguid))
422 			len += sizeof(struct scsi_vpd_id_descriptor) + 16;
423 		for (e = 0; e < sizeof(nvme_data->eui64); e++) {
424 			if (nvme_data->eui64[e] != 0)
425 				break;
426 		}
427 		if (e < sizeof(nvme_data->eui64))
428 			len += sizeof(struct scsi_vpd_id_descriptor) + 8;
429 		if (len > 0) {
430 			path->device->device_id = (u_int8_t *)
431 			    malloc(SVPD_DEVICE_ID_HDR_LEN + len,
432 			    M_CAMXPT, M_NOWAIT);
433 		}
434 		if (path->device->device_id != NULL) {
435 			did = (struct scsi_vpd_device_id *)path->device->device_id;
436 			did->device = SID_QUAL_LU_CONNECTED | T_DIRECT;
437 			did->page_code = SVPD_DEVICE_ID;
438 			scsi_ulto2b(len, did->length);
439 			idd = (struct scsi_vpd_id_descriptor *)(did + 1);
440 			if (g < sizeof(nvme_data->nguid)) {
441 				idd->proto_codeset = SVPD_ID_CODESET_BINARY;
442 				idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64;
443 				idd->length = 16;
444 				bcopy(nvme_data->nguid, idd->identifier, 16);
445 				idd = (struct scsi_vpd_id_descriptor *)
446 				    &idd->identifier[16];
447 			}
448 			if (e < sizeof(nvme_data->eui64)) {
449 				idd->proto_codeset = SVPD_ID_CODESET_BINARY;
450 				idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64;
451 				idd->length = 8;
452 				bcopy(nvme_data->eui64, idd->identifier, 8);
453 			}
454 			path->device->device_id_len = SVPD_DEVICE_ID_HDR_LEN + len;
455 		}
456 
457 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
458 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
459 			xpt_acquire_device(path->device);
460 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
461 			xpt_action(done_ccb);
462 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
463 		}
464 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
465 		break;
466 	default:
467 		panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
468 	}
469 done:
470 	if (softc->restart) {
471 		softc->restart = 0;
472 		xpt_release_ccb(done_ccb);
473 		nvme_probe_schedule(periph);
474 		goto out;
475 	}
476 	xpt_release_ccb(done_ccb);
477 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
478 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
479 		TAILQ_REMOVE(&softc->request_ccbs,
480 		    &done_ccb->ccb_h, periph_links.tqe);
481 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
482 		xpt_done(done_ccb);
483 	}
484 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
485 	cam_release_devq(path, 0, 0, 0, FALSE);
486 	cam_periph_invalidate(periph);
487 	cam_periph_release_locked(periph);
488 }
489 
490 static void
491 nvme_probe_cleanup(struct cam_periph *periph)
492 {
493 
494 	free(periph->softc, M_CAMXPT);
495 }
496 
497 #if 0
498 /* XXX should be used, don't delete */
499 static void
500 nvme_find_quirk(struct cam_ed *device)
501 {
502 	struct nvme_quirk_entry *quirk;
503 	caddr_t	match;
504 
505 	match = cam_quirkmatch((caddr_t)&device->nvme_data,
506 			       (caddr_t)nvme_quirk_table,
507 			       nvme_quirk_table_size,
508 			       sizeof(*nvme_quirk_table), nvme_identify_match);
509 
510 	if (match == NULL)
511 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
512 
513 	quirk = (struct nvme_quirk_entry *)match;
514 	device->quirk = quirk;
515 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
516 		device->mintags = quirk->mintags;
517 		device->maxtags = quirk->maxtags;
518 	}
519 }
520 #endif
521 
522 static void
523 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
524 	     cam_flags flags, union ccb *request_ccb)
525 {
526 	struct ccb_pathinq cpi;
527 	cam_status status;
528 	struct cam_periph *old_periph;
529 	int lock;
530 
531 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
532 
533 	xpt_path_inq(&cpi, path);
534 
535 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
536 		if (request_ccb != NULL) {
537 			request_ccb->ccb_h.status = cpi.ccb_h.status;
538 			xpt_done(request_ccb);
539 		}
540 		return;
541 	}
542 
543 	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
544 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
545 		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
546 		xpt_done(request_ccb);
547 		return;
548 	}
549 
550 	lock = (xpt_path_owned(path) == 0);
551 	if (lock)
552 		xpt_path_lock(path);
553 	if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
554 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
555 			nvme_probe_softc *softc;
556 
557 			softc = (nvme_probe_softc *)old_periph->softc;
558 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
559 				&request_ccb->ccb_h, periph_links.tqe);
560 			softc->restart = 1;
561 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
562 			    ("restarting nvme_probe device\n"));
563 		} else {
564 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
565 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
566 			    ("Failing to restart nvme_probe device\n"));
567 			xpt_done(request_ccb);
568 		}
569 	} else {
570 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
571 		    ("Adding nvme_probe device\n"));
572 		status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
573 					  nvme_probe_start, "nvme_probe",
574 					  CAM_PERIPH_BIO,
575 					  request_ccb->ccb_h.path, NULL, 0,
576 					  request_ccb);
577 
578 		if (status != CAM_REQ_CMP) {
579 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
580 			    "returned an error, can't continue probe\n");
581 			request_ccb->ccb_h.status = status;
582 			xpt_done(request_ccb);
583 		}
584 	}
585 	if (lock)
586 		xpt_path_unlock(path);
587 }
588 
589 static struct cam_ed *
590 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
591 {
592 	struct nvme_quirk_entry *quirk;
593 	struct cam_ed *device;
594 
595 	device = xpt_alloc_device(bus, target, lun_id);
596 	if (device == NULL)
597 		return (NULL);
598 
599 	/*
600 	 * Take the default quirk entry until we have inquiry
601 	 * data from nvme and can determine a better quirk to use.
602 	 */
603 	quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
604 	device->quirk = (void *)quirk;
605 	device->mintags = 0;
606 	device->maxtags = 0;
607 	device->inq_flags = 0;
608 	device->queue_flags = 0;
609 	device->device_id = NULL;
610 	device->device_id_len = 0;
611 	device->serial_num = NULL;
612 	device->serial_num_len = 0;
613 	return (device);
614 }
615 
616 static void
617 nvme_device_transport(struct cam_path *path)
618 {
619 	struct ccb_pathinq cpi;
620 	struct ccb_trans_settings cts;
621 	/* XXX get data from nvme namespace and other info ??? */
622 
623 	/* Get transport information from the SIM */
624 	xpt_path_inq(&cpi, path);
625 
626 	path->device->transport = cpi.transport;
627 	path->device->transport_version = cpi.transport_version;
628 
629 	path->device->protocol = cpi.protocol;
630 	path->device->protocol_version = cpi.protocol_version;
631 
632 	/* Tell the controller what we think */
633 	memset(&cts, 0, sizeof(cts));
634 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
635 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
636 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
637 	cts.transport = path->device->transport;
638 	cts.transport_version = path->device->transport_version;
639 	cts.protocol = path->device->protocol;
640 	cts.protocol_version = path->device->protocol_version;
641 	cts.proto_specific.valid = 0;
642 	cts.xport_specific.valid = 0;
643 	xpt_action((union ccb *)&cts);
644 }
645 
646 static void
647 nvme_dev_advinfo(union ccb *start_ccb)
648 {
649 	struct cam_ed *device;
650 	struct ccb_dev_advinfo *cdai;
651 	off_t amt;
652 
653 	xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
654 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
655 	device = start_ccb->ccb_h.path->device;
656 	cdai = &start_ccb->cdai;
657 	switch(cdai->buftype) {
658 	case CDAI_TYPE_SCSI_DEVID:
659 		if (cdai->flags & CDAI_FLAG_STORE)
660 			return;
661 		cdai->provsiz = device->device_id_len;
662 		if (device->device_id_len == 0)
663 			break;
664 		amt = device->device_id_len;
665 		if (cdai->provsiz > cdai->bufsiz)
666 			amt = cdai->bufsiz;
667 		memcpy(cdai->buf, device->device_id, amt);
668 		break;
669 	case CDAI_TYPE_SERIAL_NUM:
670 		if (cdai->flags & CDAI_FLAG_STORE)
671 			return;
672 		cdai->provsiz = device->serial_num_len;
673 		if (device->serial_num_len == 0)
674 			break;
675 		amt = device->serial_num_len;
676 		if (cdai->provsiz > cdai->bufsiz)
677 			amt = cdai->bufsiz;
678 		memcpy(cdai->buf, device->serial_num, amt);
679 		break;
680 	case CDAI_TYPE_PHYS_PATH:
681 		if (cdai->flags & CDAI_FLAG_STORE) {
682 			if (device->physpath != NULL) {
683 				free(device->physpath, M_CAMXPT);
684 				device->physpath = NULL;
685 				device->physpath_len = 0;
686 			}
687 			/* Clear existing buffer if zero length */
688 			if (cdai->bufsiz == 0)
689 				break;
690 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
691 			if (device->physpath == NULL) {
692 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
693 				return;
694 			}
695 			device->physpath_len = cdai->bufsiz;
696 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
697 		} else {
698 			cdai->provsiz = device->physpath_len;
699 			if (device->physpath_len == 0)
700 				break;
701 			amt = device->physpath_len;
702 			if (cdai->provsiz > cdai->bufsiz)
703 				amt = cdai->bufsiz;
704 			memcpy(cdai->buf, device->physpath, amt);
705 		}
706 		break;
707 	case CDAI_TYPE_NVME_CNTRL:
708 		if (cdai->flags & CDAI_FLAG_STORE)
709 			return;
710 		amt = sizeof(struct nvme_controller_data);
711 		cdai->provsiz = amt;
712 		if (amt > cdai->bufsiz)
713 			amt = cdai->bufsiz;
714 		memcpy(cdai->buf, device->nvme_cdata, amt);
715 		break;
716 	case CDAI_TYPE_NVME_NS:
717 		if (cdai->flags & CDAI_FLAG_STORE)
718 			return;
719 		amt = sizeof(struct nvme_namespace_data);
720 		cdai->provsiz = amt;
721 		if (amt > cdai->bufsiz)
722 			amt = cdai->bufsiz;
723 		memcpy(cdai->buf, device->nvme_data, amt);
724 		break;
725 	default:
726 		return;
727 	}
728 	start_ccb->ccb_h.status = CAM_REQ_CMP;
729 
730 	if (cdai->flags & CDAI_FLAG_STORE) {
731 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
732 			  (void *)(uintptr_t)cdai->buftype);
733 	}
734 }
735 
736 static void
737 nvme_action(union ccb *start_ccb)
738 {
739 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
740 	    ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
741 
742 	switch (start_ccb->ccb_h.func_code) {
743 	case XPT_SCAN_BUS:
744 	case XPT_SCAN_TGT:
745 	case XPT_SCAN_LUN:
746 		nvme_scan_lun(start_ccb->ccb_h.path->periph,
747 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
748 			      start_ccb);
749 		break;
750 	case XPT_DEV_ADVINFO:
751 		nvme_dev_advinfo(start_ccb);
752 		break;
753 
754 	default:
755 		xpt_action_default(start_ccb);
756 		break;
757 	}
758 }
759 
760 /*
761  * Handle any per-device event notifications that require action by the XPT.
762  */
763 static void
764 nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
765 	      struct cam_ed *device, void *async_arg)
766 {
767 
768 	/*
769 	 * We only need to handle events for real devices.
770 	 */
771 	if (target->target_id == CAM_TARGET_WILDCARD
772 	 || device->lun_id == CAM_LUN_WILDCARD)
773 		return;
774 
775 	if (async_code == AC_LOST_DEVICE &&
776 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
777 		device->flags |= CAM_DEV_UNCONFIGURED;
778 		xpt_release_device(device);
779 	}
780 }
781 
782 static void
783 nvme_announce_periph(struct cam_periph *periph)
784 {
785 	struct	ccb_pathinq cpi;
786 	struct	ccb_trans_settings cts;
787 	struct	cam_path *path = periph->path;
788 	struct ccb_trans_settings_nvme	*nvmex;
789 	struct sbuf	sb;
790 	char		buffer[120];
791 
792 	cam_periph_assert(periph, MA_OWNED);
793 
794 	/* Ask the SIM for connection details */
795 	memset(&cts, 0, sizeof(cts));
796 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
797 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
798 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
799 	xpt_action((union ccb*)&cts);
800 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
801 		return;
802 	nvmex = &cts.xport_specific.nvme;
803 
804 	/* Ask the SIM for its base transfer speed */
805 	xpt_path_inq(&cpi, periph->path);
806 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
807 	sbuf_printf(&sb, "%s%d: nvme version %d.%d",
808 	    periph->periph_name, periph->unit_number,
809 	    NVME_MAJOR(nvmex->spec),
810 	    NVME_MINOR(nvmex->spec));
811 	if (nvmex->valid & CTS_NVME_VALID_LINK)
812 		sbuf_printf(&sb, " x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
813 		    nvmex->lanes, nvmex->max_lanes,
814 		    nvmex->speed, nvmex->max_speed);
815 	sbuf_printf(&sb, "\n");
816 	sbuf_finish(&sb);
817 	sbuf_putbuf(&sb);
818 }
819 
820 static void
821 nvme_proto_announce(struct cam_ed *device)
822 {
823 	struct sbuf	sb;
824 	char		buffer[120];
825 
826 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
827 	nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
828 	sbuf_finish(&sb);
829 	sbuf_putbuf(&sb);
830 }
831 
832 static void
833 nvme_proto_denounce(struct cam_ed *device)
834 {
835 
836 	nvme_proto_announce(device);
837 }
838 
839 static void
840 nvme_proto_debug_out(union ccb *ccb)
841 {
842 	char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
843 
844 	if (ccb->ccb_h.func_code != XPT_NVME_IO &&
845 	    ccb->ccb_h.func_code != XPT_NVME_ADMIN)
846 		return;
847 
848 	CAM_DEBUG(ccb->ccb_h.path,
849 	    CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd,
850 		ccb->ccb_h.func_code == XPT_NVME_ADMIN),
851 		nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));
852 }
853