1/*-
2 * Copyright (c) 2015 Netflix, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: releng/11.0/sys/cam/nvme/nvme_xpt.c 301778 2016-06-10 06:04:53Z imp $");
31
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/endian.h>
35#include <sys/systm.h>
36#include <sys/types.h>
37#include <sys/malloc.h>
38#include <sys/kernel.h>
39#include <sys/time.h>
40#include <sys/conf.h>
41#include <sys/fcntl.h>
42#include <sys/interrupt.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,
87	NVME_PROBE_DONE,
88	NVME_PROBE_INVALID,
89	NVME_PROBE_RESET
90} nvme_probe_action;
91
92static char *nvme_probe_action_text[] = {
93	"NVME_PROBE_IDENTIFY",
94	"NVME_PROBE_DONE",
95	"NVME_PROBE_INVALID",
96	"NVME_PROBE_RESET",
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	nvme_probe_action	action;
116	nvme_probe_flags	flags;
117	int		restart;
118	struct cam_periph *periph;
119} nvme_probe_softc;
120
121static struct nvme_quirk_entry nvme_quirk_table[] =
122{
123	{
124//		{
125//		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
126//		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
127//		},
128		.quirks = 0, .mintags = 0, .maxtags = 0
129	},
130};
131
132static const int nvme_quirk_table_size =
133	sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
134
135static cam_status	nvme_probe_register(struct cam_periph *periph,
136				      void *arg);
137static void	 nvme_probe_schedule(struct cam_periph *nvme_probe_periph);
138static void	 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb);
139static void	 nvme_probe_cleanup(struct cam_periph *periph);
140//static void	 nvme_find_quirk(struct cam_ed *device);
141static void	 nvme_scan_lun(struct cam_periph *periph,
142			       struct cam_path *path, cam_flags flags,
143			       union ccb *ccb);
144static struct cam_ed *
145		 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
146				   lun_id_t lun_id);
147static void	 nvme_device_transport(struct cam_path *path);
148static void	 nvme_dev_async(u_int32_t async_code,
149				struct cam_eb *bus,
150				struct cam_et *target,
151				struct cam_ed *device,
152				void *async_arg);
153static void	 nvme_action(union ccb *start_ccb);
154static void	 nvme_announce_periph(struct cam_periph *periph);
155
156static struct xpt_xport nvme_xport = {
157	.alloc_device = nvme_alloc_device,
158	.action = nvme_action,
159	.async = nvme_dev_async,
160	.announce = nvme_announce_periph,
161};
162
163struct xpt_xport *
164nvme_get_xport(void)
165{
166	return (&nvme_xport);
167}
168
169static void
170nvme_probe_periph_init()
171{
172	printf("nvme cam probe device init\n");
173}
174
175static cam_status
176nvme_probe_register(struct cam_periph *periph, void *arg)
177{
178	union ccb *request_ccb;	/* CCB representing the probe request */
179	cam_status status;
180	nvme_probe_softc *softc;
181
182	request_ccb = (union ccb *)arg;
183	if (request_ccb == NULL) {
184		printf("nvme_probe_register: no probe CCB, "
185		       "can't register device\n");
186		return(CAM_REQ_CMP_ERR);
187	}
188
189	softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
190
191	if (softc == NULL) {
192		printf("nvme_probe_register: Unable to probe new device. "
193		       "Unable to allocate softc\n");
194		return(CAM_REQ_CMP_ERR);
195	}
196	TAILQ_INIT(&softc->request_ccbs);
197	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
198			  periph_links.tqe);
199	softc->flags = 0;
200	periph->softc = softc;
201	softc->periph = periph;
202	softc->action = NVME_PROBE_INVALID;
203	status = cam_periph_acquire(periph);
204	if (status != CAM_REQ_CMP) {
205		return (status);
206	}
207	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
208
209//	nvme_device_transport(periph->path);
210	nvme_probe_schedule(periph);
211
212	return(CAM_REQ_CMP);
213}
214
215static void
216nvme_probe_schedule(struct cam_periph *periph)
217{
218	union ccb *ccb;
219	nvme_probe_softc *softc;
220
221	softc = (nvme_probe_softc *)periph->softc;
222	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
223
224	NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY);
225
226	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
227		softc->flags |= NVME_PROBE_NO_ANNOUNCE;
228	else
229		softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
230
231	xpt_schedule(periph, CAM_PRIORITY_XPT);
232}
233
234static void
235nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
236{
237	struct ccb_nvmeio *nvmeio;
238	struct ccb_scsiio *csio;
239	nvme_probe_softc *softc;
240	struct cam_path *path;
241	const struct nvme_namespace_data *nvme_data;
242	lun_id_t lun;
243
244	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
245
246	softc = (nvme_probe_softc *)periph->softc;
247	path = start_ccb->ccb_h.path;
248	nvmeio = &start_ccb->nvmeio;
249	csio = &start_ccb->csio;
250	nvme_data = periph->path->device->nvme_data;
251
252	if (softc->restart) {
253		softc->restart = 0;
254		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
255			NVME_PROBE_SET_ACTION(softc, NVME_PROBE_RESET);
256		else
257			NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY);
258	}
259
260	/*
261	 * Other transports have to ask their SIM to do a lot of action.
262	 * NVMe doesn't, so don't do the dance. Just do things
263	 * directly.
264	 */
265	switch (softc->action) {
266	case NVME_PROBE_RESET:
267		/* FALLTHROUGH */
268	case NVME_PROBE_IDENTIFY:
269		nvme_device_transport(path);
270		/*
271		 * Test for lun == CAM_LUN_WILDCARD is lame, but
272		 * appears to be necessary here. XXX
273		 */
274		lun = xpt_path_lun_id(periph->path);
275		if (lun == CAM_LUN_WILDCARD ||
276		    periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
277			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
278			xpt_acquire_device(path->device);
279			start_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
280			xpt_action(start_ccb);
281			xpt_async(AC_FOUND_DEVICE, path, start_ccb);
282		}
283		NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
284		break;
285	default:
286		panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
287	}
288	/*
289	 * Probing is now done. We need to complete any lingering items
290	 * in the queue, though there shouldn't be any.
291	 */
292	xpt_release_ccb(start_ccb);
293	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
294	while ((start_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
295		TAILQ_REMOVE(&softc->request_ccbs,
296		    &start_ccb->ccb_h, periph_links.tqe);
297		start_ccb->ccb_h.status = CAM_REQ_CMP;
298		xpt_done(start_ccb);
299	}
300// XXX not sure I need this
301// XXX unlike other XPTs, we never freeze the queue since we have a super-simple
302// XXX state machine
303	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. -- did we really do this? */
304//	cam_release_devq(path, 0, 0, 0, FALSE);
305	cam_periph_invalidate(periph);
306	cam_periph_release_locked(periph);
307}
308
309static void
310nvme_probe_cleanup(struct cam_periph *periph)
311{
312	free(periph->softc, M_CAMXPT);
313}
314
315#if 0
316/* XXX should be used, don't delete */
317static void
318nvme_find_quirk(struct cam_ed *device)
319{
320	struct nvme_quirk_entry *quirk;
321	caddr_t	match;
322
323	match = cam_quirkmatch((caddr_t)&device->nvme_data,
324			       (caddr_t)nvme_quirk_table,
325			       nvme_quirk_table_size,
326			       sizeof(*nvme_quirk_table), nvme_identify_match);
327
328	if (match == NULL)
329		panic("xpt_find_quirk: device didn't match wildcard entry!!");
330
331	quirk = (struct nvme_quirk_entry *)match;
332	device->quirk = quirk;
333	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
334		device->mintags = quirk->mintags;
335		device->maxtags = quirk->maxtags;
336	}
337}
338#endif
339
340static void
341nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
342	     cam_flags flags, union ccb *request_ccb)
343{
344	struct ccb_pathinq cpi;
345	cam_status status;
346	struct cam_periph *old_periph;
347	int lock;
348
349	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
350
351	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
352	cpi.ccb_h.func_code = XPT_PATH_INQ;
353	xpt_action((union ccb *)&cpi);
354
355	if (cpi.ccb_h.status != CAM_REQ_CMP) {
356		if (request_ccb != NULL) {
357			request_ccb->ccb_h.status = cpi.ccb_h.status;
358			xpt_done(request_ccb);
359		}
360		return;
361	}
362
363	if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
364		CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
365		request_ccb->ccb_h.status = CAM_REQ_CMP;	/* XXX signal error ? */
366		xpt_done(request_ccb);
367		return;
368	}
369
370	lock = (xpt_path_owned(path) == 0);
371	if (lock)
372		xpt_path_lock(path);
373	if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
374		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
375			nvme_probe_softc *softc;
376
377			softc = (nvme_probe_softc *)old_periph->softc;
378			TAILQ_INSERT_TAIL(&softc->request_ccbs,
379				&request_ccb->ccb_h, periph_links.tqe);
380			softc->restart = 1;
381			CAM_DEBUG(path, CAM_DEBUG_TRACE,
382			    ("restarting nvme_probe device\n"));
383		} else {
384			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
385			CAM_DEBUG(path, CAM_DEBUG_TRACE,
386			    ("Failing to restart nvme_probe device\n"));
387			xpt_done(request_ccb);
388		}
389	} else {
390		CAM_DEBUG(path, CAM_DEBUG_TRACE,
391		    ("Adding nvme_probe device\n"));
392		status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
393					  nvme_probe_start, "nvme_probe",
394					  CAM_PERIPH_BIO,
395					  request_ccb->ccb_h.path, NULL, 0,
396					  request_ccb);
397
398		if (status != CAM_REQ_CMP) {
399			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
400			    "returned an error, can't continue probe\n");
401			request_ccb->ccb_h.status = status;
402			xpt_done(request_ccb);
403		}
404	}
405	if (lock)
406		xpt_path_unlock(path);
407}
408
409static struct cam_ed *
410nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
411{
412	struct nvme_quirk_entry *quirk;
413	struct cam_ed *device;
414
415	device = xpt_alloc_device(bus, target, lun_id);
416	if (device == NULL)
417		return (NULL);
418
419	/*
420	 * Take the default quirk entry until we have inquiry
421	 * data from nvme and can determine a better quirk to use.
422	 */
423	quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
424	device->quirk = (void *)quirk;
425	device->mintags = 0;
426	device->maxtags = 0;
427	device->inq_flags = 0;
428	device->queue_flags = 0;
429	device->device_id = NULL;	/* XXX Need to set this somewhere */
430	device->device_id_len = 0;
431	device->serial_num = NULL;	/* XXX Need to set this somewhere */
432	device->serial_num_len = 0;
433	return (device);
434}
435
436static void
437nvme_device_transport(struct cam_path *path)
438{
439	struct ccb_pathinq cpi;
440	struct ccb_trans_settings cts;
441	/* XXX get data from nvme namespace and other info ??? */
442
443	/* Get transport information from the SIM */
444	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
445	cpi.ccb_h.func_code = XPT_PATH_INQ;
446	xpt_action((union ccb *)&cpi);
447
448	path->device->transport = cpi.transport;
449	path->device->transport_version = cpi.transport_version;
450
451	path->device->protocol = cpi.protocol;
452	path->device->protocol_version = cpi.protocol_version;
453
454	/* Tell the controller what we think */
455	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
456	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
457	cts.type = CTS_TYPE_CURRENT_SETTINGS;
458	cts.transport = path->device->transport;
459	cts.transport_version = path->device->transport_version;
460	cts.protocol = path->device->protocol;
461	cts.protocol_version = path->device->protocol_version;
462	cts.proto_specific.valid = 0;
463	cts.xport_specific.valid = 0;
464	xpt_action((union ccb *)&cts);
465}
466
467static void
468nvme_dev_advinfo(union ccb *start_ccb)
469{
470	struct cam_ed *device;
471	struct ccb_dev_advinfo *cdai;
472	off_t amt;
473
474	start_ccb->ccb_h.status = CAM_REQ_INVALID;
475	device = start_ccb->ccb_h.path->device;
476	cdai = &start_ccb->cdai;
477	switch(cdai->buftype) {
478	case CDAI_TYPE_SCSI_DEVID:
479		if (cdai->flags & CDAI_FLAG_STORE)
480			return;
481		cdai->provsiz = device->device_id_len;
482		if (device->device_id_len == 0)
483			break;
484		amt = device->device_id_len;
485		if (cdai->provsiz > cdai->bufsiz)
486			amt = cdai->bufsiz;
487		memcpy(cdai->buf, device->device_id, amt);
488		break;
489	case CDAI_TYPE_SERIAL_NUM:
490		if (cdai->flags & CDAI_FLAG_STORE)
491			return;
492		cdai->provsiz = device->serial_num_len;
493		if (device->serial_num_len == 0)
494			break;
495		amt = device->serial_num_len;
496		if (cdai->provsiz > cdai->bufsiz)
497			amt = cdai->bufsiz;
498		memcpy(cdai->buf, device->serial_num, amt);
499		break;
500	case CDAI_TYPE_PHYS_PATH:
501		if (cdai->flags & CDAI_FLAG_STORE) {
502			if (device->physpath != NULL)
503				free(device->physpath, M_CAMXPT);
504			device->physpath_len = cdai->bufsiz;
505			/* Clear existing buffer if zero length */
506			if (cdai->bufsiz == 0)
507				break;
508			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
509			if (device->physpath == NULL) {
510				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
511				return;
512			}
513			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
514		} else {
515			cdai->provsiz = device->physpath_len;
516			if (device->physpath_len == 0)
517				break;
518			amt = device->physpath_len;
519			if (cdai->provsiz > cdai->bufsiz)
520				amt = cdai->bufsiz;
521			memcpy(cdai->buf, device->physpath, amt);
522		}
523		break;
524	default:
525		return;
526	}
527	start_ccb->ccb_h.status = CAM_REQ_CMP;
528
529	if (cdai->flags & CDAI_FLAG_STORE) {
530		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
531			  (void *)(uintptr_t)cdai->buftype);
532	}
533}
534
535static void
536nvme_action(union ccb *start_ccb)
537{
538	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
539	    ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
540
541	switch (start_ccb->ccb_h.func_code) {
542	case XPT_SCAN_BUS:
543		printf("NVME scan BUS started -- ignored\n");
544//		break;
545	case XPT_SCAN_TGT:
546		printf("NVME scan TGT started -- ignored\n");
547//		break;
548	case XPT_SCAN_LUN:
549		printf("NVME scan started\n");
550		nvme_scan_lun(start_ccb->ccb_h.path->periph,
551			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
552			      start_ccb);
553		break;
554	case XPT_DEV_ADVINFO:
555		nvme_dev_advinfo(start_ccb);
556		break;
557
558	default:
559		xpt_action_default(start_ccb);
560		break;
561	}
562}
563
564/*
565 * Handle any per-device event notifications that require action by the XPT.
566 */
567static void
568nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
569	      struct cam_ed *device, void *async_arg)
570{
571
572	/*
573	 * We only need to handle events for real devices.
574	 */
575	if (target->target_id == CAM_TARGET_WILDCARD
576	 || device->lun_id == CAM_LUN_WILDCARD)
577		return;
578
579	if (async_code == AC_LOST_DEVICE &&
580	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
581		device->flags |= CAM_DEV_UNCONFIGURED;
582		xpt_release_device(device);
583	}
584}
585
586static void
587nvme_announce_periph(struct cam_periph *periph)
588{
589	struct	ccb_pathinq cpi;
590	struct	ccb_trans_settings cts;
591	struct	cam_path *path = periph->path;
592
593	cam_periph_assert(periph, MA_OWNED);
594
595	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
596	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
597	cts.type = CTS_TYPE_CURRENT_SETTINGS;
598	xpt_action((union ccb*)&cts);
599	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
600		return;
601	/* Ask the SIM for its base transfer speed */
602	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
603	cpi.ccb_h.func_code = XPT_PATH_INQ;
604	xpt_action((union ccb *)&cpi);
605	/* XXX NVME STUFF HERE */
606	printf("\n");
607}
608