Home | History | Annotate | Line # | Download | only in ic
nvmevar.h revision 1.25
      1  1.25   mlelstv /*	$NetBSD: nvmevar.h,v 1.25 2022/08/01 07:34:28 mlelstv Exp $	*/
      2   1.1    nonaka /*	$OpenBSD: nvmevar.h,v 1.8 2016/04/14 11:18:32 dlg Exp $ */
      3   1.1    nonaka 
      4   1.1    nonaka /*
      5   1.1    nonaka  * Copyright (c) 2014 David Gwynne <dlg (at) openbsd.org>
      6   1.1    nonaka  *
      7   1.1    nonaka  * Permission to use, copy, modify, and distribute this software for any
      8   1.1    nonaka  * purpose with or without fee is hereby granted, provided that the above
      9   1.1    nonaka  * copyright notice and this permission notice appear in all copies.
     10   1.1    nonaka  *
     11   1.1    nonaka  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1    nonaka  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1    nonaka  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1    nonaka  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1    nonaka  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1    nonaka  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1    nonaka  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1    nonaka  */
     19   1.1    nonaka 
     20   1.1    nonaka #include <sys/bus.h>
     21   1.1    nonaka #include <sys/cpu.h>
     22   1.1    nonaka #include <sys/device.h>
     23   1.1    nonaka #include <sys/mutex.h>
     24   1.1    nonaka #include <sys/pool.h>
     25   1.1    nonaka #include <sys/queue.h>
     26   1.1    nonaka 
     27   1.1    nonaka struct nvme_dmamem {
     28   1.1    nonaka 	bus_dmamap_t		ndm_map;
     29   1.1    nonaka 	bus_dma_segment_t	ndm_seg;
     30   1.1    nonaka 	size_t			ndm_size;
     31   1.1    nonaka 	void			*ndm_kva;
     32   1.1    nonaka };
     33   1.9  jdolecek #define NVME_DMA_MAP(_ndm)	((_ndm)->ndm_map)
     34   1.9  jdolecek #define NVME_DMA_LEN(_ndm)	((_ndm)->ndm_map->dm_segs[0].ds_len)
     35   1.9  jdolecek #define NVME_DMA_DVA(_ndm)	((uint64_t)(_ndm)->ndm_map->dm_segs[0].ds_addr)
     36   1.9  jdolecek #define NVME_DMA_KVA(_ndm)	((void *)(_ndm)->ndm_kva)
     37   1.1    nonaka 
     38   1.1    nonaka struct nvme_softc;
     39   1.1    nonaka struct nvme_queue;
     40   1.1    nonaka 
     41  1.12  jdolecek typedef void (*nvme_nnc_done)(void *, struct buf *, uint16_t, uint32_t);
     42   1.4  jdolecek 
     43   1.1    nonaka struct nvme_ccb {
     44   1.1    nonaka 	SIMPLEQ_ENTRY(nvme_ccb)	ccb_entry;
     45   1.1    nonaka 
     46   1.4  jdolecek 	/* DMA handles */
     47   1.1    nonaka 	bus_dmamap_t		ccb_dmamap;
     48   1.1    nonaka 
     49   1.4  jdolecek 	bus_addr_t		ccb_prpl_off;
     50   1.4  jdolecek 	uint64_t		ccb_prpl_dva;
     51   1.4  jdolecek 	uint64_t		*ccb_prpl;
     52   1.4  jdolecek 
     53   1.4  jdolecek 	/* command context */
     54   1.4  jdolecek 	uint16_t		ccb_id;
     55   1.1    nonaka 	void			*ccb_cookie;
     56   1.8  jdolecek #define NVME_CCB_FREE	0xbeefdeed
     57   1.1    nonaka 	void			(*ccb_done)(struct nvme_queue *,
     58   1.1    nonaka 				    struct nvme_ccb *, struct nvme_cqe *);
     59   1.1    nonaka 
     60   1.4  jdolecek 	/* namespace context */
     61   1.4  jdolecek 	void		*nnc_cookie;
     62   1.4  jdolecek 	nvme_nnc_done	nnc_done;
     63   1.4  jdolecek 	uint16_t	nnc_nsid;
     64   1.4  jdolecek 	uint16_t	nnc_flags;
     65   1.4  jdolecek #define	NVME_NS_CTX_F_READ	__BIT(0)
     66   1.4  jdolecek #define	NVME_NS_CTX_F_POLL	__BIT(1)
     67  1.13  jdolecek #define	NVME_NS_CTX_F_FUA	__BIT(2)
     68   1.1    nonaka 
     69   1.4  jdolecek 	struct buf	*nnc_buf;
     70   1.4  jdolecek 	daddr_t		nnc_blkno;
     71   1.4  jdolecek 	size_t		nnc_datasize;
     72   1.4  jdolecek 	int		nnc_secsize;
     73   1.1    nonaka };
     74   1.1    nonaka 
     75   1.1    nonaka struct nvme_queue {
     76   1.1    nonaka 	struct nvme_softc	*q_sc;
     77   1.1    nonaka 	kmutex_t		q_sq_mtx;
     78   1.1    nonaka 	kmutex_t		q_cq_mtx;
     79   1.9  jdolecek 	struct nvme_dmamem	*q_sq_dmamem;
     80   1.9  jdolecek 	struct nvme_dmamem	*q_cq_dmamem;
     81  1.24     skrll 	struct nvme_dmamem	*q_nvmmu_dmamem; /* for apple m1 nvme */
     82  1.24     skrll 
     83   1.1    nonaka 	bus_size_t 		q_sqtdbl; /* submission queue tail doorbell */
     84   1.1    nonaka 	bus_size_t 		q_cqhdbl; /* completion queue head doorbell */
     85   1.1    nonaka 	uint16_t		q_id;
     86   1.1    nonaka 	uint32_t		q_entries;
     87   1.1    nonaka 	uint32_t		q_sq_tail;
     88   1.1    nonaka 	uint32_t		q_cq_head;
     89   1.1    nonaka 	uint16_t		q_cq_phase;
     90   1.1    nonaka 
     91   1.1    nonaka 	kmutex_t		q_ccb_mtx;
     92  1.15  jdolecek 	kcondvar_t		q_ccb_wait;	/* wait for ccb avail/finish */
     93  1.15  jdolecek 	bool			q_ccb_waiting;	/* whether there are waiters */
     94  1.10  jdolecek 	uint16_t		q_nccbs;	/* total number of ccbs */
     95   1.1    nonaka 	struct nvme_ccb		*q_ccbs;
     96   1.4  jdolecek 	SIMPLEQ_HEAD(, nvme_ccb) q_ccb_list;
     97   1.9  jdolecek 	struct nvme_dmamem	*q_ccb_prpls;
     98   1.1    nonaka };
     99   1.1    nonaka 
    100   1.1    nonaka struct nvme_namespace {
    101   1.1    nonaka 	struct nvm_identify_namespace *ident;
    102   1.1    nonaka 	device_t dev;
    103   1.2    nonaka 	uint32_t flags;
    104   1.2    nonaka #define	NVME_NS_F_OPEN	__BIT(0)
    105   1.1    nonaka };
    106   1.1    nonaka 
    107  1.24     skrll struct nvme_ops {
    108  1.24     skrll 	void		(*op_enable)(struct nvme_softc *);
    109  1.24     skrll 
    110  1.24     skrll 	int		(*op_q_alloc)(struct nvme_softc *,
    111  1.24     skrll 			      struct nvme_queue *);
    112  1.24     skrll 	void		(*op_q_free)(struct nvme_softc *,
    113  1.24     skrll 			      struct nvme_queue *);
    114  1.24     skrll 
    115  1.24     skrll 	uint32_t	(*op_sq_enter)(struct nvme_softc *,
    116  1.24     skrll 			      struct nvme_queue *, struct nvme_ccb *);
    117  1.24     skrll 	void		(*op_sq_leave)(struct nvme_softc *,
    118  1.24     skrll 			      struct nvme_queue *, struct nvme_ccb *);
    119  1.24     skrll 	uint32_t	(*op_sq_enter_locked)(struct nvme_softc *,
    120  1.24     skrll 			      struct nvme_queue *, struct nvme_ccb *);
    121  1.24     skrll 	void		(*op_sq_leave_locked)(struct nvme_softc *,
    122  1.24     skrll 			      struct nvme_queue *, struct nvme_ccb *);
    123  1.24     skrll 
    124  1.24     skrll 	void		(*op_cq_done)(struct nvme_softc *,
    125  1.24     skrll 			      struct nvme_queue *, struct nvme_ccb *);
    126  1.24     skrll };
    127  1.24     skrll 
    128   1.1    nonaka struct nvme_softc {
    129   1.1    nonaka 	device_t		sc_dev;
    130   1.1    nonaka 
    131  1.24     skrll 	const struct nvme_ops	*sc_ops;
    132  1.24     skrll 
    133   1.1    nonaka 	bus_space_tag_t		sc_iot;
    134   1.1    nonaka 	bus_space_handle_t	sc_ioh;
    135   1.1    nonaka 	bus_size_t		sc_ios;
    136   1.1    nonaka 	bus_dma_tag_t		sc_dmat;
    137   1.1    nonaka 
    138   1.1    nonaka 	int			(*sc_intr_establish)(struct nvme_softc *,
    139   1.1    nonaka 				    uint16_t qid, struct nvme_queue *);
    140   1.1    nonaka 	int			(*sc_intr_disestablish)(struct nvme_softc *,
    141   1.1    nonaka 				    uint16_t qid);
    142   1.3  jdolecek 	void			**sc_ih;	/* interrupt handlers */
    143   1.3  jdolecek 	void			**sc_softih;	/* softintr handlers */
    144   1.1    nonaka 
    145   1.3  jdolecek 	u_int			sc_rdy_to;	/* RDY timeout */
    146  1.23     skrll 	size_t			sc_mps;		/* memory page size */
    147   1.3  jdolecek 	size_t			sc_mdts;	/* max data trasfer size */
    148   1.3  jdolecek 	u_int			sc_max_sgl;	/* max S/G segments */
    149  1.22  riastrad 	u_int			sc_dstrd;
    150   1.1    nonaka 
    151   1.1    nonaka 	struct nvm_identify_controller
    152   1.1    nonaka 				sc_identify;
    153   1.1    nonaka 
    154   1.3  jdolecek 	u_int			sc_nn;		/* namespace count */
    155   1.1    nonaka 	struct nvme_namespace	*sc_namespaces;
    156   1.1    nonaka 
    157   1.1    nonaka 	bool			sc_use_mq;
    158   1.1    nonaka 	u_int			sc_nq;		/* # of io queue (sc_q) */
    159   1.1    nonaka 	struct nvme_queue	*sc_admin_q;
    160   1.1    nonaka 	struct nvme_queue	**sc_q;
    161   1.1    nonaka 
    162   1.1    nonaka 	uint32_t		sc_flags;
    163   1.1    nonaka #define	NVME_F_ATTACHED	__BIT(0)
    164   1.2    nonaka #define	NVME_F_OPEN	__BIT(1)
    165  1.16    nonaka 
    166  1.16    nonaka 	uint32_t		sc_quirks;
    167  1.16    nonaka #define	NVME_QUIRK_DELAY_B4_CHK_RDY	__BIT(0)
    168  1.21  jdolecek #define	NVME_QUIRK_NOMSI		__BIT(1)
    169  1.19   mlelstv 
    170  1.19   mlelstv 	char			sc_modelname[81];
    171   1.1    nonaka };
    172   1.1    nonaka 
    173   1.1    nonaka #define	lemtoh16(p)	le16toh(*((uint16_t *)(p)))
    174   1.1    nonaka #define	lemtoh32(p)	le32toh(*((uint32_t *)(p)))
    175   1.1    nonaka #define	lemtoh64(p)	le64toh(*((uint64_t *)(p)))
    176   1.1    nonaka #define	htolem16(p, x)	(*((uint16_t *)(p)) = htole16(x))
    177   1.1    nonaka #define	htolem32(p, x)	(*((uint32_t *)(p)) = htole32(x))
    178   1.1    nonaka #define	htolem64(p, x)	(*((uint64_t *)(p)) = htole64(x))
    179   1.1    nonaka 
    180   1.1    nonaka struct nvme_attach_args {
    181   1.1    nonaka 	uint16_t	naa_nsid;
    182  1.11  jdolecek 	uint32_t	naa_qentries;	/* total number of queue slots */
    183  1.11  jdolecek 	uint32_t	naa_maxphys;	/* maximum device transfer size */
    184  1.19   mlelstv 	const char	*naa_typename;	/* identifier */
    185   1.1    nonaka };
    186   1.1    nonaka 
    187   1.1    nonaka int	nvme_attach(struct nvme_softc *);
    188   1.1    nonaka int	nvme_detach(struct nvme_softc *, int flags);
    189   1.6  pgoyette int	nvme_rescan(device_t, const char *, const int *);
    190   1.1    nonaka void	nvme_childdet(device_t, device_t);
    191  1.22  riastrad int	nvme_suspend(struct nvme_softc *);
    192  1.22  riastrad int	nvme_resume(struct nvme_softc *);
    193   1.1    nonaka int	nvme_intr(void *);
    194   1.7  jdolecek void	nvme_softintr_intx(void *);
    195   1.3  jdolecek int	nvme_intr_msi(void *);
    196   1.3  jdolecek void	nvme_softintr_msi(void *);
    197   1.1    nonaka 
    198  1.17  christos static __inline struct nvme_queue *
    199  1.20  jmcneill nvme_get_q(struct nvme_softc *sc, struct buf *bp, bool waitok)
    200   1.1    nonaka {
    201  1.25   mlelstv 	struct nvme_queue *q;
    202  1.25   mlelstv 	u_int cpunum;
    203  1.20  jmcneill 
    204  1.25   mlelstv 	cpunum = cpu_index(curcpu());
    205  1.25   mlelstv 
    206  1.25   mlelstv 	/* try own queue */
    207  1.25   mlelstv 	q = sc->sc_q[cpunum % sc->sc_nq];
    208  1.25   mlelstv 	if (waitok)
    209  1.25   mlelstv 		return q;
    210  1.25   mlelstv 
    211  1.25   mlelstv 	/* if busy, search for an idle queue */
    212  1.25   mlelstv 	if (SIMPLEQ_EMPTY(&q->q_ccb_list)) {
    213  1.25   mlelstv 		for (u_int qoff = 1; qoff < sc->sc_nq; qoff++) {
    214  1.25   mlelstv 			struct nvme_queue *t;
    215  1.25   mlelstv 			t = sc->sc_q[(cpunum + qoff) % sc->sc_nq];
    216  1.25   mlelstv 			if (t->q_sq_tail == t->q_cq_head) {
    217  1.25   mlelstv 				q = t;
    218  1.25   mlelstv 				break;
    219  1.25   mlelstv 			}
    220  1.25   mlelstv 		}
    221  1.20  jmcneill 	}
    222  1.25   mlelstv 
    223  1.25   mlelstv 	return q;
    224   1.1    nonaka }
    225   1.1    nonaka 
    226   1.1    nonaka /*
    227   1.1    nonaka  * namespace
    228   1.1    nonaka  */
    229  1.17  christos static __inline struct nvme_namespace *
    230   1.1    nonaka nvme_ns_get(struct nvme_softc *sc, uint16_t nsid)
    231   1.1    nonaka {
    232   1.1    nonaka 	if (nsid == 0 || nsid - 1 >= sc->sc_nn)
    233   1.1    nonaka 		return NULL;
    234   1.1    nonaka 	return &sc->sc_namespaces[nsid - 1];
    235   1.1    nonaka }
    236   1.1    nonaka 
    237  1.24     skrll #define nvme_read4(_s, _r) \
    238  1.24     skrll 	bus_space_read_4((_s)->sc_iot, (_s)->sc_ioh, (_r))
    239  1.24     skrll #define nvme_write4(_s, _r, _v) \
    240  1.24     skrll 	bus_space_write_4((_s)->sc_iot, (_s)->sc_ioh, (_r), (_v))
    241  1.24     skrll uint64_t
    242  1.24     skrll 	nvme_read8(struct nvme_softc *, bus_size_t);
    243  1.24     skrll void	nvme_write8(struct nvme_softc *, bus_size_t, uint64_t);
    244  1.24     skrll 
    245  1.24     skrll #define nvme_barrier(_s, _r, _l, _f) \
    246  1.24     skrll 	bus_space_barrier((_s)->sc_iot, (_s)->sc_ioh, (_r), (_l), (_f))
    247  1.24     skrll 
    248  1.24     skrll struct nvme_dmamem *
    249  1.24     skrll 	nvme_dmamem_alloc(struct nvme_softc *, size_t);
    250  1.24     skrll void	nvme_dmamem_free(struct nvme_softc *, struct nvme_dmamem *);
    251  1.24     skrll void	nvme_dmamem_sync(struct nvme_softc *, struct nvme_dmamem *, int);
    252  1.24     skrll 
    253   1.1    nonaka int	nvme_ns_identify(struct nvme_softc *, uint16_t);
    254   1.1    nonaka void	nvme_ns_free(struct nvme_softc *, uint16_t);
    255   1.4  jdolecek int	nvme_ns_dobio(struct nvme_softc *, uint16_t, void *,
    256   1.4  jdolecek     struct buf *, void *, size_t, int, daddr_t, int, nvme_nnc_done);
    257  1.15  jdolecek int	nvme_ns_sync(struct nvme_softc *, uint16_t, int);
    258  1.15  jdolecek int	nvme_admin_getcache(struct nvme_softc *, int *);
    259  1.18  jdolecek int	nvme_admin_setcache(struct nvme_softc *, int);
    260