xref: /freebsd/sys/cam/nvme/nvme_xpt.c (revision e40d8dbb)
1baabaca3SWarner Losh /*-
2f24882ecSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3f24882ecSPedro F. Giffuni  *
4baabaca3SWarner Losh  * Copyright (c) 2015 Netflix, Inc.
5baabaca3SWarner Losh  *
6baabaca3SWarner Losh  * Redistribution and use in source and binary forms, with or without
7baabaca3SWarner Losh  * modification, are permitted provided that the following conditions
8baabaca3SWarner Losh  * are met:
9baabaca3SWarner Losh  * 1. Redistributions of source code must retain the above copyright
10baabaca3SWarner Losh  *    notice, this list of conditions and the following disclaimer,
11baabaca3SWarner Losh  *    without modification, immediately at the beginning of the file.
12baabaca3SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13baabaca3SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14baabaca3SWarner Losh  *    documentation and/or other materials provided with the distribution.
15baabaca3SWarner Losh  *
16baabaca3SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17baabaca3SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18baabaca3SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19baabaca3SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20baabaca3SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21baabaca3SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22baabaca3SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23baabaca3SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24baabaca3SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25baabaca3SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26baabaca3SWarner Losh  *
27baabaca3SWarner Losh  * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
28baabaca3SWarner Losh  */
29baabaca3SWarner Losh 
30baabaca3SWarner Losh #include <sys/cdefs.h>
31baabaca3SWarner Losh __FBSDID("$FreeBSD$");
32baabaca3SWarner Losh 
33baabaca3SWarner Losh #include <sys/param.h>
34baabaca3SWarner Losh #include <sys/bus.h>
35baabaca3SWarner Losh #include <sys/endian.h>
36baabaca3SWarner Losh #include <sys/systm.h>
37baabaca3SWarner Losh #include <sys/types.h>
38baabaca3SWarner Losh #include <sys/malloc.h>
39baabaca3SWarner Losh #include <sys/kernel.h>
40baabaca3SWarner Losh #include <sys/time.h>
41baabaca3SWarner Losh #include <sys/conf.h>
42baabaca3SWarner Losh #include <sys/fcntl.h>
43baabaca3SWarner Losh #include <sys/sbuf.h>
44baabaca3SWarner Losh 
45baabaca3SWarner Losh #include <sys/lock.h>
46baabaca3SWarner Losh #include <sys/mutex.h>
47baabaca3SWarner Losh #include <sys/sysctl.h>
48baabaca3SWarner Losh 
49baabaca3SWarner Losh #include <cam/cam.h>
50baabaca3SWarner Losh #include <cam/cam_ccb.h>
51baabaca3SWarner Losh #include <cam/cam_queue.h>
52baabaca3SWarner Losh #include <cam/cam_periph.h>
53baabaca3SWarner Losh #include <cam/cam_sim.h>
54baabaca3SWarner Losh #include <cam/cam_xpt.h>
55baabaca3SWarner Losh #include <cam/cam_xpt_sim.h>
56baabaca3SWarner Losh #include <cam/cam_xpt_periph.h>
57baabaca3SWarner Losh #include <cam/cam_xpt_internal.h>
58baabaca3SWarner Losh #include <cam/cam_debug.h>
59baabaca3SWarner Losh 
60baabaca3SWarner Losh #include <cam/scsi/scsi_all.h>
61baabaca3SWarner Losh #include <cam/scsi/scsi_message.h>
62baabaca3SWarner Losh #include <cam/nvme/nvme_all.h>
63baabaca3SWarner Losh #include <machine/stdarg.h>	/* for xpt_print below */
64baabaca3SWarner Losh #include "opt_cam.h"
65baabaca3SWarner Losh 
66baabaca3SWarner Losh struct nvme_quirk_entry {
67baabaca3SWarner Losh 	u_int quirks;
68baabaca3SWarner Losh #define CAM_QUIRK_MAXTAGS 1
69baabaca3SWarner Losh 	u_int mintags;
70baabaca3SWarner Losh 	u_int maxtags;
71baabaca3SWarner Losh };
72baabaca3SWarner Losh 
73baabaca3SWarner Losh /* Not even sure why we need this */
74baabaca3SWarner Losh static periph_init_t nvme_probe_periph_init;
75baabaca3SWarner Losh 
76baabaca3SWarner Losh static struct periph_driver nvme_probe_driver =
77baabaca3SWarner Losh {
78baabaca3SWarner Losh 	nvme_probe_periph_init, "nvme_probe",
79baabaca3SWarner Losh 	TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
80baabaca3SWarner Losh 	CAM_PERIPH_DRV_EARLY
81baabaca3SWarner Losh };
82baabaca3SWarner Losh 
83baabaca3SWarner Losh PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
84baabaca3SWarner Losh 
85baabaca3SWarner Losh typedef enum {
86f439e3a4SAlexander Motin 	NVME_PROBE_IDENTIFY_CD,
87f439e3a4SAlexander Motin 	NVME_PROBE_IDENTIFY_NS,
88baabaca3SWarner Losh 	NVME_PROBE_DONE,
89f439e3a4SAlexander Motin 	NVME_PROBE_INVALID
90baabaca3SWarner Losh } nvme_probe_action;
91baabaca3SWarner Losh 
92baabaca3SWarner Losh static char *nvme_probe_action_text[] = {
93f439e3a4SAlexander Motin 	"NVME_PROBE_IDENTIFY_CD",
94f439e3a4SAlexander Motin 	"NVME_PROBE_IDENTIFY_NS",
95baabaca3SWarner Losh 	"NVME_PROBE_DONE",
96f439e3a4SAlexander Motin 	"NVME_PROBE_INVALID"
97baabaca3SWarner Losh };
98baabaca3SWarner Losh 
99baabaca3SWarner Losh #define NVME_PROBE_SET_ACTION(softc, newaction)	\
100baabaca3SWarner Losh do {									\
101baabaca3SWarner Losh 	char **text;							\
102baabaca3SWarner Losh 	text = nvme_probe_action_text;					\
103baabaca3SWarner Losh 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
104baabaca3SWarner Losh 	    ("Probe %s to %s\n", text[(softc)->action],			\
105baabaca3SWarner Losh 	    text[(newaction)]));					\
106baabaca3SWarner Losh 	(softc)->action = (newaction);					\
107baabaca3SWarner Losh } while(0)
108baabaca3SWarner Losh 
109baabaca3SWarner Losh typedef enum {
110baabaca3SWarner Losh 	NVME_PROBE_NO_ANNOUNCE	= 0x04
111baabaca3SWarner Losh } nvme_probe_flags;
112baabaca3SWarner Losh 
113baabaca3SWarner Losh typedef struct {
114baabaca3SWarner Losh 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
115f439e3a4SAlexander Motin 	union {
116f439e3a4SAlexander Motin 		struct nvme_controller_data	cd;
117f439e3a4SAlexander Motin 		struct nvme_namespace_data	ns;
118f439e3a4SAlexander Motin 	};
119baabaca3SWarner Losh 	nvme_probe_action	action;
120baabaca3SWarner Losh 	nvme_probe_flags	flags;
121baabaca3SWarner Losh 	int		restart;
122baabaca3SWarner Losh 	struct cam_periph *periph;
123baabaca3SWarner Losh } nvme_probe_softc;
124baabaca3SWarner Losh 
125baabaca3SWarner Losh static struct nvme_quirk_entry nvme_quirk_table[] =
126baabaca3SWarner Losh {
127baabaca3SWarner Losh 	{
128baabaca3SWarner Losh //		{
129baabaca3SWarner Losh //		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
130baabaca3SWarner Losh //		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
131baabaca3SWarner Losh //		},
132baabaca3SWarner Losh 		.quirks = 0, .mintags = 0, .maxtags = 0
133baabaca3SWarner Losh 	},
134baabaca3SWarner Losh };
135baabaca3SWarner Losh 
136baabaca3SWarner Losh static const int nvme_quirk_table_size =
137baabaca3SWarner Losh 	sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
138baabaca3SWarner Losh 
139baabaca3SWarner Losh static cam_status	nvme_probe_register(struct cam_periph *periph,
140baabaca3SWarner Losh 				      void *arg);
141baabaca3SWarner Losh static void	 nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
142baabaca3SWarner Losh static void	 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
143f439e3a4SAlexander Motin static void	 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb);
144baabaca3SWarner Losh static void	 nvme_probe_cleanup(struct cam_periph *periph);
145f24c011bSWarner Losh //static void	 nvme_find_quirk(struct cam_ed *device);
146baabaca3SWarner Losh static void	 nvme_scan_lun(struct cam_periph *periph,
147baabaca3SWarner Losh 			       struct cam_path *path, cam_flags flags,
148baabaca3SWarner Losh 			       union ccb *ccb);
149baabaca3SWarner Losh static struct cam_ed *
150baabaca3SWarner Losh 		 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
151baabaca3SWarner Losh 				   lun_id_t lun_id);
152baabaca3SWarner Losh static void	 nvme_device_transport(struct cam_path *path);
153baabaca3SWarner Losh static void	 nvme_dev_async(u_int32_t async_code,
154baabaca3SWarner Losh 				struct cam_eb *bus,
155baabaca3SWarner Losh 				struct cam_et *target,
156baabaca3SWarner Losh 				struct cam_ed *device,
157baabaca3SWarner Losh 				void *async_arg);
158baabaca3SWarner Losh static void	 nvme_action(union ccb *start_ccb);
159baabaca3SWarner Losh static void	 nvme_announce_periph(struct cam_periph *periph);
16008f13879SWarner Losh static void	 nvme_proto_announce(struct cam_ed *device);
16108f13879SWarner Losh static void	 nvme_proto_denounce(struct cam_ed *device);
16208f13879SWarner Losh static void	 nvme_proto_debug_out(union ccb *ccb);
163baabaca3SWarner Losh 
164ded2b706SWarner Losh static struct xpt_xport_ops nvme_xport_ops = {
165baabaca3SWarner Losh 	.alloc_device = nvme_alloc_device,
166baabaca3SWarner Losh 	.action = nvme_action,
167baabaca3SWarner Losh 	.async = nvme_dev_async,
168baabaca3SWarner Losh 	.announce = nvme_announce_periph,
169baabaca3SWarner Losh };
170ded2b706SWarner Losh #define NVME_XPT_XPORT(x, X)			\
171ded2b706SWarner Losh static struct xpt_xport nvme_xport_ ## x = {	\
172ded2b706SWarner Losh 	.xport = XPORT_ ## X,			\
173ded2b706SWarner Losh 	.name = #x,				\
174ded2b706SWarner Losh 	.ops = &nvme_xport_ops,			\
175ded2b706SWarner Losh };						\
176ded2b706SWarner Losh CAM_XPT_XPORT(nvme_xport_ ## x);
177baabaca3SWarner Losh 
178ded2b706SWarner Losh NVME_XPT_XPORT(nvme, NVME);
179e0ce51cbSWarner Losh 
180ded2b706SWarner Losh #undef NVME_XPT_XPORT
181baabaca3SWarner Losh 
18208f13879SWarner Losh static struct xpt_proto_ops nvme_proto_ops = {
18308f13879SWarner Losh 	.announce = nvme_proto_announce,
18408f13879SWarner Losh 	.denounce = nvme_proto_denounce,
18508f13879SWarner Losh 	.debug_out = nvme_proto_debug_out,
18608f13879SWarner Losh };
18708f13879SWarner Losh static struct xpt_proto nvme_proto = {
18808f13879SWarner Losh 	.proto = PROTO_NVME,
18908f13879SWarner Losh 	.name = "nvme",
19008f13879SWarner Losh 	.ops = &nvme_proto_ops,
19108f13879SWarner Losh };
19208f13879SWarner Losh CAM_XPT_PROTO(nvme_proto);
19308f13879SWarner Losh 
194baabaca3SWarner Losh static void
195baabaca3SWarner Losh nvme_probe_periph_init()
196baabaca3SWarner Losh {
197e0ce51cbSWarner Losh 
198baabaca3SWarner Losh }
199baabaca3SWarner Losh 
200baabaca3SWarner Losh static cam_status
201baabaca3SWarner Losh nvme_probe_register(struct cam_periph *periph, void *arg)
202baabaca3SWarner Losh {
203baabaca3SWarner Losh 	union ccb *request_ccb;	/* CCB representing the probe request */
204baabaca3SWarner Losh 	nvme_probe_softc *softc;
205baabaca3SWarner Losh 
206baabaca3SWarner Losh 	request_ccb = (union ccb *)arg;
207baabaca3SWarner Losh 	if (request_ccb == NULL) {
208baabaca3SWarner Losh 		printf("nvme_probe_register: no probe CCB, "
209baabaca3SWarner Losh 		       "can't register device\n");
210baabaca3SWarner Losh 		return(CAM_REQ_CMP_ERR);
211baabaca3SWarner Losh 	}
212baabaca3SWarner Losh 
213baabaca3SWarner Losh 	softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
214baabaca3SWarner Losh 
215baabaca3SWarner Losh 	if (softc == NULL) {
216baabaca3SWarner Losh 		printf("nvme_probe_register: Unable to probe new device. "
217baabaca3SWarner Losh 		       "Unable to allocate softc\n");
218baabaca3SWarner Losh 		return(CAM_REQ_CMP_ERR);
219baabaca3SWarner Losh 	}
220baabaca3SWarner Losh 	TAILQ_INIT(&softc->request_ccbs);
221baabaca3SWarner Losh 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
222baabaca3SWarner Losh 			  periph_links.tqe);
223baabaca3SWarner Losh 	softc->flags = 0;
224baabaca3SWarner Losh 	periph->softc = softc;
225baabaca3SWarner Losh 	softc->periph = periph;
226baabaca3SWarner Losh 	softc->action = NVME_PROBE_INVALID;
22799e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0)
22899e7a4adSScott Long 		return (CAM_REQ_CMP_ERR);
22999e7a4adSScott Long 
230baabaca3SWarner Losh 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
231baabaca3SWarner Losh 
232baabaca3SWarner Losh //	nvme_device_transport(periph->path);
233baabaca3SWarner Losh 	nvme_probe_schedule(periph);
234baabaca3SWarner Losh 
235baabaca3SWarner Losh 	return(CAM_REQ_CMP);
236baabaca3SWarner Losh }
237baabaca3SWarner Losh 
238baabaca3SWarner Losh static void
239baabaca3SWarner Losh nvme_probe_schedule(struct cam_periph *periph)
240baabaca3SWarner Losh {
241baabaca3SWarner Losh 	union ccb *ccb;
242baabaca3SWarner Losh 	nvme_probe_softc *softc;
243baabaca3SWarner Losh 
244baabaca3SWarner Losh 	softc = (nvme_probe_softc *)periph->softc;
245baabaca3SWarner Losh 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
246baabaca3SWarner Losh 
247f439e3a4SAlexander Motin 	NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
248baabaca3SWarner Losh 
249baabaca3SWarner Losh 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
250baabaca3SWarner Losh 		softc->flags |= NVME_PROBE_NO_ANNOUNCE;
251baabaca3SWarner Losh 	else
252baabaca3SWarner Losh 		softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
253baabaca3SWarner Losh 
254baabaca3SWarner Losh 	xpt_schedule(periph, CAM_PRIORITY_XPT);
255baabaca3SWarner Losh }
256baabaca3SWarner Losh 
257baabaca3SWarner Losh static void
258baabaca3SWarner Losh nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
259baabaca3SWarner Losh {
260baabaca3SWarner Losh 	struct ccb_nvmeio *nvmeio;
261baabaca3SWarner Losh 	nvme_probe_softc *softc;
262baabaca3SWarner Losh 	struct cam_path *path;
263baabaca3SWarner Losh 	lun_id_t lun;
264baabaca3SWarner Losh 
265baabaca3SWarner Losh 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
266baabaca3SWarner Losh 
267baabaca3SWarner Losh 	softc = (nvme_probe_softc *)periph->softc;
268baabaca3SWarner Losh 	path = start_ccb->ccb_h.path;
269baabaca3SWarner Losh 	nvmeio = &start_ccb->nvmeio;
270f439e3a4SAlexander Motin 	lun = xpt_path_lun_id(periph->path);
271baabaca3SWarner Losh 
272baabaca3SWarner Losh 	if (softc->restart) {
273baabaca3SWarner Losh 		softc->restart = 0;
274f439e3a4SAlexander Motin 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
275baabaca3SWarner Losh 	}
276baabaca3SWarner Losh 
277baabaca3SWarner Losh 	switch (softc->action) {
278f439e3a4SAlexander Motin 	case NVME_PROBE_IDENTIFY_CD:
279f439e3a4SAlexander Motin 		cam_fill_nvmeadmin(nvmeio,
280f439e3a4SAlexander Motin 		    0,			/* retries */
281f439e3a4SAlexander Motin 		    nvme_probe_done,	/* cbfcnp */
282f439e3a4SAlexander Motin 		    CAM_DIR_IN,		/* flags */
283f439e3a4SAlexander Motin 		    (uint8_t *)&softc->cd,	/* data_ptr */
284f439e3a4SAlexander Motin 		    sizeof(softc->cd),		/* dxfer_len */
285f439e3a4SAlexander Motin 		    30 * 1000); /* timeout 30s */
286f439e3a4SAlexander Motin 		nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0,
287f439e3a4SAlexander Motin 		    1, 0, 0, 0, 0, 0);
288f439e3a4SAlexander Motin 		break;
289f439e3a4SAlexander Motin 	case NVME_PROBE_IDENTIFY_NS:
290f439e3a4SAlexander Motin 		cam_fill_nvmeadmin(nvmeio,
291f439e3a4SAlexander Motin 		    0,			/* retries */
292f439e3a4SAlexander Motin 		    nvme_probe_done,	/* cbfcnp */
293f439e3a4SAlexander Motin 		    CAM_DIR_IN,		/* flags */
294f439e3a4SAlexander Motin 		    (uint8_t *)&softc->ns,	/* data_ptr */
295f439e3a4SAlexander Motin 		    sizeof(softc->ns),		/* dxfer_len */
296f439e3a4SAlexander Motin 		    30 * 1000); /* timeout 30s */
297f439e3a4SAlexander Motin 		nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun,
298f439e3a4SAlexander Motin 		    0, 0, 0, 0, 0, 0);
299baabaca3SWarner Losh 		break;
300baabaca3SWarner Losh 	default:
301baabaca3SWarner Losh 		panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
302baabaca3SWarner Losh 	}
303f439e3a4SAlexander Motin 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
304f439e3a4SAlexander Motin 	xpt_action(start_ccb);
305baabaca3SWarner Losh }
306f439e3a4SAlexander Motin 
307f439e3a4SAlexander Motin static void
308f439e3a4SAlexander Motin nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
309f439e3a4SAlexander Motin {
310f439e3a4SAlexander Motin 	struct nvme_namespace_data *nvme_data;
311f439e3a4SAlexander Motin 	struct nvme_controller_data *nvme_cdata;
312f439e3a4SAlexander Motin 	nvme_probe_softc *softc;
313f439e3a4SAlexander Motin 	struct cam_path *path;
314f439e3a4SAlexander Motin 	cam_status status;
315f439e3a4SAlexander Motin 	u_int32_t  priority;
316f439e3a4SAlexander Motin 	int found = 1;
317f439e3a4SAlexander Motin 
318f439e3a4SAlexander Motin 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n"));
319f439e3a4SAlexander Motin 
320f439e3a4SAlexander Motin 	softc = (nvme_probe_softc *)periph->softc;
321f439e3a4SAlexander Motin 	path = done_ccb->ccb_h.path;
322f439e3a4SAlexander Motin 	priority = done_ccb->ccb_h.pinfo.priority;
323f439e3a4SAlexander Motin 
324f439e3a4SAlexander Motin 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
325f439e3a4SAlexander Motin 		if (cam_periph_error(done_ccb,
326f439e3a4SAlexander Motin 			0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
327f439e3a4SAlexander Motin 		    ) == ERESTART) {
328f439e3a4SAlexander Motin out:
329f439e3a4SAlexander Motin 			/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
330f439e3a4SAlexander Motin 			cam_release_devq(path, 0, 0, 0, FALSE);
331f439e3a4SAlexander Motin 			return;
332f439e3a4SAlexander Motin 		}
333f439e3a4SAlexander Motin 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
334f439e3a4SAlexander Motin 			/* Don't wedge the queue */
335f439e3a4SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
336f439e3a4SAlexander Motin 		}
337f439e3a4SAlexander Motin 		status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
338f439e3a4SAlexander Motin 
339f439e3a4SAlexander Motin 		/*
340f439e3a4SAlexander Motin 		 * If we get to this point, we got an error status back
341f439e3a4SAlexander Motin 		 * from the inquiry and the error status doesn't require
342f439e3a4SAlexander Motin 		 * automatically retrying the command.  Therefore, the
343f439e3a4SAlexander Motin 		 * inquiry failed.  If we had inquiry information before
344f439e3a4SAlexander Motin 		 * for this device, but this latest inquiry command failed,
345f439e3a4SAlexander Motin 		 * the device has probably gone away.  If this device isn't
346f439e3a4SAlexander Motin 		 * already marked unconfigured, notify the peripheral
347f439e3a4SAlexander Motin 		 * drivers that this device is no more.
348f439e3a4SAlexander Motin 		 */
349f439e3a4SAlexander Motin device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
350f439e3a4SAlexander Motin 			xpt_async(AC_LOST_DEVICE, path, NULL);
351f439e3a4SAlexander Motin 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID);
352f439e3a4SAlexander Motin 		found = 0;
353f439e3a4SAlexander Motin 		goto done;
354f439e3a4SAlexander Motin 	}
355f439e3a4SAlexander Motin 	if (softc->restart)
356f439e3a4SAlexander Motin 		goto done;
357f439e3a4SAlexander Motin 	switch (softc->action) {
358f439e3a4SAlexander Motin 	case NVME_PROBE_IDENTIFY_CD:
359f439e3a4SAlexander Motin 		nvme_controller_data_swapbytes(&softc->cd);
360f439e3a4SAlexander Motin 
361f439e3a4SAlexander Motin 		nvme_cdata = path->device->nvme_cdata;
362f439e3a4SAlexander Motin 		if (nvme_cdata == NULL) {
363f439e3a4SAlexander Motin 			nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT,
364f439e3a4SAlexander Motin 			    M_NOWAIT);
365f439e3a4SAlexander Motin 			if (nvme_cdata == NULL) {
366f439e3a4SAlexander Motin 				xpt_print(path, "Can't allocate memory");
367f439e3a4SAlexander Motin 				goto device_fail;
368f439e3a4SAlexander Motin 			}
369f439e3a4SAlexander Motin 		}
370f439e3a4SAlexander Motin 		bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata));
371f439e3a4SAlexander Motin 		path->device->nvme_cdata = nvme_cdata;
372f439e3a4SAlexander Motin 
373f439e3a4SAlexander Motin //		nvme_find_quirk(path->device);
374f439e3a4SAlexander Motin 		nvme_device_transport(path);
375f439e3a4SAlexander Motin 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS);
376f439e3a4SAlexander Motin 		xpt_release_ccb(done_ccb);
377f439e3a4SAlexander Motin 		xpt_schedule(periph, priority);
378f439e3a4SAlexander Motin 		goto out;
379f439e3a4SAlexander Motin 	case NVME_PROBE_IDENTIFY_NS:
380f439e3a4SAlexander Motin 		nvme_namespace_data_swapbytes(&softc->ns);
381f439e3a4SAlexander Motin 
382f439e3a4SAlexander Motin 		/* Check that the namespace exists. */
383f439e3a4SAlexander Motin 		if (softc->ns.nsze == 0)
384f439e3a4SAlexander Motin 			goto device_fail;
385f439e3a4SAlexander Motin 
386f439e3a4SAlexander Motin 		nvme_data = path->device->nvme_data;
387f439e3a4SAlexander Motin 		if (nvme_data == NULL) {
388f439e3a4SAlexander Motin 			nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
389f439e3a4SAlexander Motin 			    M_NOWAIT);
390f439e3a4SAlexander Motin 			if (nvme_data == NULL) {
391f439e3a4SAlexander Motin 				xpt_print(path, "Can't allocate memory");
392f439e3a4SAlexander Motin 				goto device_fail;
393f439e3a4SAlexander Motin 			}
394f439e3a4SAlexander Motin 		}
395f439e3a4SAlexander Motin 		bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
396f439e3a4SAlexander Motin 		path->device->nvme_data = nvme_data;
397f439e3a4SAlexander Motin 
398f439e3a4SAlexander Motin 		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
399f439e3a4SAlexander Motin 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
400f439e3a4SAlexander Motin 			xpt_acquire_device(path->device);
401f439e3a4SAlexander Motin 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
402f439e3a4SAlexander Motin 			xpt_action(done_ccb);
403f439e3a4SAlexander Motin 			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
404f439e3a4SAlexander Motin 		}
405f439e3a4SAlexander Motin 		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
406f439e3a4SAlexander Motin 		break;
407f439e3a4SAlexander Motin 	default:
408f439e3a4SAlexander Motin 		panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
409f439e3a4SAlexander Motin 	}
410f439e3a4SAlexander Motin done:
411f439e3a4SAlexander Motin 	if (softc->restart) {
412f439e3a4SAlexander Motin 		softc->restart = 0;
413f439e3a4SAlexander Motin 		xpt_release_ccb(done_ccb);
414f439e3a4SAlexander Motin 		nvme_probe_schedule(periph);
415f439e3a4SAlexander Motin 		goto out;
416f439e3a4SAlexander Motin 	}
417f439e3a4SAlexander Motin 	xpt_release_ccb(done_ccb);
418f439e3a4SAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
419f439e3a4SAlexander Motin 	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
420f439e3a4SAlexander Motin 		TAILQ_REMOVE(&softc->request_ccbs,
421f439e3a4SAlexander Motin 		    &done_ccb->ccb_h, periph_links.tqe);
422f439e3a4SAlexander Motin 		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
423f439e3a4SAlexander Motin 		xpt_done(done_ccb);
424f439e3a4SAlexander Motin 	}
425f439e3a4SAlexander Motin 	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
426f439e3a4SAlexander Motin 	cam_release_devq(path, 0, 0, 0, FALSE);
427baabaca3SWarner Losh 	cam_periph_invalidate(periph);
428876f6a6aSScott Long 	cam_periph_release_locked(periph);
429baabaca3SWarner Losh }
430baabaca3SWarner Losh 
431baabaca3SWarner Losh static void
432baabaca3SWarner Losh nvme_probe_cleanup(struct cam_periph *periph)
433baabaca3SWarner Losh {
434e0ce51cbSWarner Losh 
435baabaca3SWarner Losh 	free(periph->softc, M_CAMXPT);
436baabaca3SWarner Losh }
437baabaca3SWarner Losh 
438f24c011bSWarner Losh #if 0
439baabaca3SWarner Losh /* XXX should be used, don't delete */
440baabaca3SWarner Losh static void
441baabaca3SWarner Losh nvme_find_quirk(struct cam_ed *device)
442baabaca3SWarner Losh {
443baabaca3SWarner Losh 	struct nvme_quirk_entry *quirk;
444baabaca3SWarner Losh 	caddr_t	match;
445baabaca3SWarner Losh 
446baabaca3SWarner Losh 	match = cam_quirkmatch((caddr_t)&device->nvme_data,
447baabaca3SWarner Losh 			       (caddr_t)nvme_quirk_table,
448baabaca3SWarner Losh 			       nvme_quirk_table_size,
449baabaca3SWarner Losh 			       sizeof(*nvme_quirk_table), nvme_identify_match);
450baabaca3SWarner Losh 
451baabaca3SWarner Losh 	if (match == NULL)
452baabaca3SWarner Losh 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
453baabaca3SWarner Losh 
454baabaca3SWarner Losh 	quirk = (struct nvme_quirk_entry *)match;
455baabaca3SWarner Losh 	device->quirk = quirk;
456baabaca3SWarner Losh 	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
457baabaca3SWarner Losh 		device->mintags = quirk->mintags;
458baabaca3SWarner Losh 		device->maxtags = quirk->maxtags;
459baabaca3SWarner Losh 	}
460baabaca3SWarner Losh }
461f24c011bSWarner Losh #endif
462baabaca3SWarner Losh 
463baabaca3SWarner Losh static void
464baabaca3SWarner Losh nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
465baabaca3SWarner Losh 	     cam_flags flags, union ccb *request_ccb)
466baabaca3SWarner Losh {
467baabaca3SWarner Losh 	struct ccb_pathinq cpi;
468baabaca3SWarner Losh 	cam_status status;
469baabaca3SWarner Losh 	struct cam_periph *old_periph;
470baabaca3SWarner Losh 	int lock;
471baabaca3SWarner Losh 
472baabaca3SWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
473baabaca3SWarner Losh 
474762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
475baabaca3SWarner Losh 
476baabaca3SWarner Losh 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
477baabaca3SWarner Losh 		if (request_ccb != NULL) {
478baabaca3SWarner Losh 			request_ccb->ccb_h.status = cpi.ccb_h.status;
479baabaca3SWarner Losh 			xpt_done(request_ccb);
480baabaca3SWarner Losh 		}
481baabaca3SWarner Losh 		return;
482baabaca3SWarner Losh 	}
483baabaca3SWarner Losh 
484baabaca3SWarner Losh 	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
485baabaca3SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
486baabaca3SWarner Losh 		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
487baabaca3SWarner Losh 		xpt_done(request_ccb);
488baabaca3SWarner Losh 		return;
489baabaca3SWarner Losh 	}
490baabaca3SWarner Losh 
491baabaca3SWarner Losh 	lock = (xpt_path_owned(path) == 0);
492baabaca3SWarner Losh 	if (lock)
493baabaca3SWarner Losh 		xpt_path_lock(path);
494baabaca3SWarner Losh 	if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
495baabaca3SWarner Losh 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
496baabaca3SWarner Losh 			nvme_probe_softc *softc;
497baabaca3SWarner Losh 
498baabaca3SWarner Losh 			softc = (nvme_probe_softc *)old_periph->softc;
499baabaca3SWarner Losh 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
500baabaca3SWarner Losh 				&request_ccb->ccb_h, periph_links.tqe);
501baabaca3SWarner Losh 			softc->restart = 1;
502baabaca3SWarner Losh 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
503baabaca3SWarner Losh 			    ("restarting nvme_probe device\n"));
504baabaca3SWarner Losh 		} else {
505baabaca3SWarner Losh 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
506baabaca3SWarner Losh 			CAM_DEBUG(path, CAM_DEBUG_TRACE,
507baabaca3SWarner Losh 			    ("Failing to restart nvme_probe device\n"));
508baabaca3SWarner Losh 			xpt_done(request_ccb);
509baabaca3SWarner Losh 		}
510baabaca3SWarner Losh 	} else {
511baabaca3SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
512baabaca3SWarner Losh 		    ("Adding nvme_probe device\n"));
513baabaca3SWarner Losh 		status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
514baabaca3SWarner Losh 					  nvme_probe_start, "nvme_probe",
515baabaca3SWarner Losh 					  CAM_PERIPH_BIO,
516baabaca3SWarner Losh 					  request_ccb->ccb_h.path, NULL, 0,
517baabaca3SWarner Losh 					  request_ccb);
518baabaca3SWarner Losh 
519baabaca3SWarner Losh 		if (status != CAM_REQ_CMP) {
520baabaca3SWarner Losh 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
521baabaca3SWarner Losh 			    "returned an error, can't continue probe\n");
522baabaca3SWarner Losh 			request_ccb->ccb_h.status = status;
523baabaca3SWarner Losh 			xpt_done(request_ccb);
524baabaca3SWarner Losh 		}
525baabaca3SWarner Losh 	}
526baabaca3SWarner Losh 	if (lock)
527baabaca3SWarner Losh 		xpt_path_unlock(path);
528baabaca3SWarner Losh }
529baabaca3SWarner Losh 
530baabaca3SWarner Losh static struct cam_ed *
531baabaca3SWarner Losh nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
532baabaca3SWarner Losh {
533baabaca3SWarner Losh 	struct nvme_quirk_entry *quirk;
534baabaca3SWarner Losh 	struct cam_ed *device;
535baabaca3SWarner Losh 
536baabaca3SWarner Losh 	device = xpt_alloc_device(bus, target, lun_id);
537baabaca3SWarner Losh 	if (device == NULL)
538baabaca3SWarner Losh 		return (NULL);
539baabaca3SWarner Losh 
540baabaca3SWarner Losh 	/*
541baabaca3SWarner Losh 	 * Take the default quirk entry until we have inquiry
542baabaca3SWarner Losh 	 * data from nvme and can determine a better quirk to use.
543baabaca3SWarner Losh 	 */
544baabaca3SWarner Losh 	quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
545baabaca3SWarner Losh 	device->quirk = (void *)quirk;
546baabaca3SWarner Losh 	device->mintags = 0;
547baabaca3SWarner Losh 	device->maxtags = 0;
548baabaca3SWarner Losh 	device->inq_flags = 0;
549baabaca3SWarner Losh 	device->queue_flags = 0;
550baabaca3SWarner Losh 	device->device_id = NULL;	/* XXX Need to set this somewhere */
551baabaca3SWarner Losh 	device->device_id_len = 0;
552baabaca3SWarner Losh 	device->serial_num = NULL;	/* XXX Need to set this somewhere */
553baabaca3SWarner Losh 	device->serial_num_len = 0;
554baabaca3SWarner Losh 	return (device);
555baabaca3SWarner Losh }
556baabaca3SWarner Losh 
557baabaca3SWarner Losh static void
558baabaca3SWarner Losh nvme_device_transport(struct cam_path *path)
559baabaca3SWarner Losh {
560baabaca3SWarner Losh 	struct ccb_pathinq cpi;
561baabaca3SWarner Losh 	struct ccb_trans_settings cts;
562baabaca3SWarner Losh 	/* XXX get data from nvme namespace and other info ??? */
563baabaca3SWarner Losh 
564baabaca3SWarner Losh 	/* Get transport information from the SIM */
565762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
566baabaca3SWarner Losh 
567baabaca3SWarner Losh 	path->device->transport = cpi.transport;
568baabaca3SWarner Losh 	path->device->transport_version = cpi.transport_version;
569baabaca3SWarner Losh 
570baabaca3SWarner Losh 	path->device->protocol = cpi.protocol;
571baabaca3SWarner Losh 	path->device->protocol_version = cpi.protocol_version;
572baabaca3SWarner Losh 
573baabaca3SWarner Losh 	/* Tell the controller what we think */
574baabaca3SWarner Losh 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
575baabaca3SWarner Losh 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
576baabaca3SWarner Losh 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
577baabaca3SWarner Losh 	cts.transport = path->device->transport;
578baabaca3SWarner Losh 	cts.transport_version = path->device->transport_version;
579baabaca3SWarner Losh 	cts.protocol = path->device->protocol;
580baabaca3SWarner Losh 	cts.protocol_version = path->device->protocol_version;
581baabaca3SWarner Losh 	cts.proto_specific.valid = 0;
582baabaca3SWarner Losh 	cts.xport_specific.valid = 0;
583baabaca3SWarner Losh 	xpt_action((union ccb *)&cts);
584baabaca3SWarner Losh }
585baabaca3SWarner Losh 
586baabaca3SWarner Losh static void
587baabaca3SWarner Losh nvme_dev_advinfo(union ccb *start_ccb)
588baabaca3SWarner Losh {
589baabaca3SWarner Losh 	struct cam_ed *device;
590baabaca3SWarner Losh 	struct ccb_dev_advinfo *cdai;
591baabaca3SWarner Losh 	off_t amt;
592baabaca3SWarner Losh 
593baabaca3SWarner Losh 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
594baabaca3SWarner Losh 	device = start_ccb->ccb_h.path->device;
595baabaca3SWarner Losh 	cdai = &start_ccb->cdai;
596baabaca3SWarner Losh 	switch(cdai->buftype) {
597baabaca3SWarner Losh 	case CDAI_TYPE_SCSI_DEVID:
598baabaca3SWarner Losh 		if (cdai->flags & CDAI_FLAG_STORE)
599baabaca3SWarner Losh 			return;
600baabaca3SWarner Losh 		cdai->provsiz = device->device_id_len;
601baabaca3SWarner Losh 		if (device->device_id_len == 0)
602baabaca3SWarner Losh 			break;
603baabaca3SWarner Losh 		amt = device->device_id_len;
604baabaca3SWarner Losh 		if (cdai->provsiz > cdai->bufsiz)
605baabaca3SWarner Losh 			amt = cdai->bufsiz;
606baabaca3SWarner Losh 		memcpy(cdai->buf, device->device_id, amt);
607baabaca3SWarner Losh 		break;
608baabaca3SWarner Losh 	case CDAI_TYPE_SERIAL_NUM:
609baabaca3SWarner Losh 		if (cdai->flags & CDAI_FLAG_STORE)
610baabaca3SWarner Losh 			return;
611baabaca3SWarner Losh 		cdai->provsiz = device->serial_num_len;
612baabaca3SWarner Losh 		if (device->serial_num_len == 0)
613baabaca3SWarner Losh 			break;
614baabaca3SWarner Losh 		amt = device->serial_num_len;
615baabaca3SWarner Losh 		if (cdai->provsiz > cdai->bufsiz)
616baabaca3SWarner Losh 			amt = cdai->bufsiz;
617baabaca3SWarner Losh 		memcpy(cdai->buf, device->serial_num, amt);
618baabaca3SWarner Losh 		break;
619baabaca3SWarner Losh 	case CDAI_TYPE_PHYS_PATH:
620baabaca3SWarner Losh 		if (cdai->flags & CDAI_FLAG_STORE) {
621baabaca3SWarner Losh 			if (device->physpath != NULL)
622baabaca3SWarner Losh 				free(device->physpath, M_CAMXPT);
623baabaca3SWarner Losh 			device->physpath_len = cdai->bufsiz;
624baabaca3SWarner Losh 			/* Clear existing buffer if zero length */
625baabaca3SWarner Losh 			if (cdai->bufsiz == 0)
626baabaca3SWarner Losh 				break;
627baabaca3SWarner Losh 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
628baabaca3SWarner Losh 			if (device->physpath == NULL) {
629baabaca3SWarner Losh 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
630baabaca3SWarner Losh 				return;
631baabaca3SWarner Losh 			}
632baabaca3SWarner Losh 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
633baabaca3SWarner Losh 		} else {
634baabaca3SWarner Losh 			cdai->provsiz = device->physpath_len;
635baabaca3SWarner Losh 			if (device->physpath_len == 0)
636baabaca3SWarner Losh 				break;
637baabaca3SWarner Losh 			amt = device->physpath_len;
638baabaca3SWarner Losh 			if (cdai->provsiz > cdai->bufsiz)
639baabaca3SWarner Losh 				amt = cdai->bufsiz;
640baabaca3SWarner Losh 			memcpy(cdai->buf, device->physpath, amt);
641baabaca3SWarner Losh 		}
642baabaca3SWarner Losh 		break;
6439f8ed7e4SWarner Losh 	case CDAI_TYPE_NVME_CNTRL:
6449f8ed7e4SWarner Losh 		if (cdai->flags & CDAI_FLAG_STORE)
6459f8ed7e4SWarner Losh 			return;
6469f8ed7e4SWarner Losh 		amt = sizeof(struct nvme_controller_data);
6479f8ed7e4SWarner Losh 		cdai->provsiz = amt;
6489f8ed7e4SWarner Losh 		if (amt > cdai->bufsiz)
6499f8ed7e4SWarner Losh 			amt = cdai->bufsiz;
6509f8ed7e4SWarner Losh 		memcpy(cdai->buf, device->nvme_cdata, amt);
6519f8ed7e4SWarner Losh 		break;
6529f8ed7e4SWarner Losh 	case CDAI_TYPE_NVME_NS:
6539f8ed7e4SWarner Losh 		if (cdai->flags & CDAI_FLAG_STORE)
6549f8ed7e4SWarner Losh 			return;
6559f8ed7e4SWarner Losh 		amt = sizeof(struct nvme_namespace_data);
6569f8ed7e4SWarner Losh 		cdai->provsiz = amt;
6579f8ed7e4SWarner Losh 		if (amt > cdai->bufsiz)
6589f8ed7e4SWarner Losh 			amt = cdai->bufsiz;
6599f8ed7e4SWarner Losh 		memcpy(cdai->buf, device->nvme_data, amt);
6609f8ed7e4SWarner Losh 		break;
661baabaca3SWarner Losh 	default:
662baabaca3SWarner Losh 		return;
663baabaca3SWarner Losh 	}
664baabaca3SWarner Losh 	start_ccb->ccb_h.status = CAM_REQ_CMP;
665baabaca3SWarner Losh 
666baabaca3SWarner Losh 	if (cdai->flags & CDAI_FLAG_STORE) {
667baabaca3SWarner Losh 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
668baabaca3SWarner Losh 			  (void *)(uintptr_t)cdai->buftype);
669baabaca3SWarner Losh 	}
670baabaca3SWarner Losh }
671baabaca3SWarner Losh 
672baabaca3SWarner Losh static void
673baabaca3SWarner Losh nvme_action(union ccb *start_ccb)
674baabaca3SWarner Losh {
675baabaca3SWarner Losh 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
676baabaca3SWarner Losh 	    ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
677baabaca3SWarner Losh 
678baabaca3SWarner Losh 	switch (start_ccb->ccb_h.func_code) {
679baabaca3SWarner Losh 	case XPT_SCAN_BUS:
680baabaca3SWarner Losh 	case XPT_SCAN_TGT:
681baabaca3SWarner Losh 	case XPT_SCAN_LUN:
682baabaca3SWarner Losh 		nvme_scan_lun(start_ccb->ccb_h.path->periph,
683baabaca3SWarner Losh 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
684baabaca3SWarner Losh 			      start_ccb);
685baabaca3SWarner Losh 		break;
686baabaca3SWarner Losh 	case XPT_DEV_ADVINFO:
687baabaca3SWarner Losh 		nvme_dev_advinfo(start_ccb);
688baabaca3SWarner Losh 		break;
689baabaca3SWarner Losh 
690baabaca3SWarner Losh 	default:
691baabaca3SWarner Losh 		xpt_action_default(start_ccb);
692baabaca3SWarner Losh 		break;
693baabaca3SWarner Losh 	}
694baabaca3SWarner Losh }
695baabaca3SWarner Losh 
696baabaca3SWarner Losh /*
697baabaca3SWarner Losh  * Handle any per-device event notifications that require action by the XPT.
698baabaca3SWarner Losh  */
699baabaca3SWarner Losh static void
700baabaca3SWarner Losh nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
701baabaca3SWarner Losh 	      struct cam_ed *device, void *async_arg)
702baabaca3SWarner Losh {
703baabaca3SWarner Losh 
704baabaca3SWarner Losh 	/*
705baabaca3SWarner Losh 	 * We only need to handle events for real devices.
706baabaca3SWarner Losh 	 */
707baabaca3SWarner Losh 	if (target->target_id == CAM_TARGET_WILDCARD
708baabaca3SWarner Losh 	 || device->lun_id == CAM_LUN_WILDCARD)
709baabaca3SWarner Losh 		return;
710baabaca3SWarner Losh 
711baabaca3SWarner Losh 	if (async_code == AC_LOST_DEVICE &&
712baabaca3SWarner Losh 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
713baabaca3SWarner Losh 		device->flags |= CAM_DEV_UNCONFIGURED;
714baabaca3SWarner Losh 		xpt_release_device(device);
715baabaca3SWarner Losh 	}
716baabaca3SWarner Losh }
717baabaca3SWarner Losh 
718baabaca3SWarner Losh static void
719baabaca3SWarner Losh nvme_announce_periph(struct cam_periph *periph)
720baabaca3SWarner Losh {
721baabaca3SWarner Losh 	struct	ccb_pathinq cpi;
722baabaca3SWarner Losh 	struct	ccb_trans_settings cts;
723baabaca3SWarner Losh 	struct	cam_path *path = periph->path;
7245e8a39f6SWarner Losh 	struct ccb_trans_settings_nvme	*nvmex;
725baabaca3SWarner Losh 
726baabaca3SWarner Losh 	cam_periph_assert(periph, MA_OWNED);
727baabaca3SWarner Losh 
7285e8a39f6SWarner Losh 	/* Ask the SIM for connection details */
729baabaca3SWarner Losh 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
730baabaca3SWarner Losh 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
731baabaca3SWarner Losh 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
732baabaca3SWarner Losh 	xpt_action((union ccb*)&cts);
733baabaca3SWarner Losh 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
734baabaca3SWarner Losh 		return;
7355e8a39f6SWarner Losh 	nvmex = &cts.xport_specific.nvme;
7365e8a39f6SWarner Losh 
737baabaca3SWarner Losh 	/* Ask the SIM for its base transfer speed */
738762a7f4fSWarner Losh 	xpt_path_inq(&cpi, periph->path);
7395e8a39f6SWarner Losh 	printf("%s%d: nvme version %d.%d x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
7405e8a39f6SWarner Losh 	    periph->periph_name, periph->unit_number,
7415e8a39f6SWarner Losh 	    NVME_MAJOR(nvmex->spec),
7425e8a39f6SWarner Losh 	    NVME_MINOR(nvmex->spec),
7435e8a39f6SWarner Losh 	    nvmex->lanes, nvmex->max_lanes,
7445e8a39f6SWarner Losh 	    nvmex->speed, nvmex->max_speed);
745baabaca3SWarner Losh 	printf("\n");
746baabaca3SWarner Losh }
74708f13879SWarner Losh 
74808f13879SWarner Losh static void
74908f13879SWarner Losh nvme_proto_announce(struct cam_ed *device)
75008f13879SWarner Losh {
7515e8a39f6SWarner Losh 	struct sbuf	sb;
7525e8a39f6SWarner Losh 	char		buffer[120];
7534e3b2744SWarner Losh 
7545e8a39f6SWarner Losh 	sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
7555e8a39f6SWarner Losh 	nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
7565e8a39f6SWarner Losh 	sbuf_finish(&sb);
7575e8a39f6SWarner Losh 	sbuf_putbuf(&sb);
75808f13879SWarner Losh }
75908f13879SWarner Losh 
76008f13879SWarner Losh static void
76108f13879SWarner Losh nvme_proto_denounce(struct cam_ed *device)
76208f13879SWarner Losh {
7634e3b2744SWarner Losh 
7645e8a39f6SWarner Losh 	nvme_proto_announce(device);
76508f13879SWarner Losh }
76608f13879SWarner Losh 
76708f13879SWarner Losh static void
76808f13879SWarner Losh nvme_proto_debug_out(union ccb *ccb)
76908f13879SWarner Losh {
77008f13879SWarner Losh 	char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
77108f13879SWarner Losh 
772e40d8dbbSAlexander Motin 	if (ccb->ccb_h.func_code != XPT_NVME_IO ||
773e40d8dbbSAlexander Motin 	    ccb->ccb_h.func_code != XPT_NVME_ADMIN)
77408f13879SWarner Losh 		return;
77508f13879SWarner Losh 
77608f13879SWarner Losh 	CAM_DEBUG(ccb->ccb_h.path,
777e40d8dbbSAlexander Motin 	    CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd,
778e40d8dbbSAlexander Motin 		ccb->ccb_h.func_code == XPT_NVME_ADMIN),
77908f13879SWarner Losh 		nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));
78008f13879SWarner Losh }
78108f13879SWarner Losh 
782