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