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