Home | History | Annotate | Line # | Download | only in ic
      1  1.4   msaitoh /*	$NetBSD: nvmeio.h,v 1.4 2021/11/10 17:19:30 msaitoh Exp $	*/
      2  1.1    nonaka 
      3  1.1    nonaka /*-
      4  1.1    nonaka  * Copyright (C) 2012-2013 Intel Corporation
      5  1.1    nonaka  * All rights reserved.
      6  1.1    nonaka  *
      7  1.1    nonaka  * Redistribution and use in source and binary forms, with or without
      8  1.1    nonaka  * modification, are permitted provided that the following conditions
      9  1.1    nonaka  * are met:
     10  1.1    nonaka  * 1. Redistributions of source code must retain the above copyright
     11  1.1    nonaka  *    notice, this list of conditions and the following disclaimer.
     12  1.1    nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    nonaka  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    nonaka  *    documentation and/or other materials provided with the distribution.
     15  1.1    nonaka  *
     16  1.1    nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1    nonaka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1    nonaka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1    nonaka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1    nonaka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1    nonaka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1    nonaka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1    nonaka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1    nonaka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1    nonaka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1    nonaka  * SUCH DAMAGE.
     27  1.1    nonaka  *
     28  1.2    nonaka  * $FreeBSD: head/sys/dev/nvme/nvme.h 329824 2018-02-22 13:32:31Z wma $
     29  1.1    nonaka  */
     30  1.1    nonaka 
     31  1.1    nonaka #ifndef __NVMEIO_H__
     32  1.1    nonaka #define __NVMEIO_H__
     33  1.1    nonaka 
     34  1.2    nonaka #include <sys/endian.h>
     35  1.1    nonaka #include <sys/ioccom.h>
     36  1.1    nonaka #include <dev/ic/nvmereg.h>
     37  1.1    nonaka 
     38  1.1    nonaka #define	NVME_PASSTHROUGH_CMD		_IOWR('n', 0, struct nvme_pt_command)
     39  1.1    nonaka 
     40  1.1    nonaka #define	nvme_completion_is_error(cpl)					\
     41  1.1    nonaka 	((NVME_CQE_SC((cpl)->flags) != NVME_CQE_SC_SUCCESS)   		\
     42  1.1    nonaka 	    || (NVME_CQE_SCT((cpl)->flags) != NVME_CQE_SCT_GENERIC))
     43  1.1    nonaka 
     44  1.1    nonaka struct nvme_pt_command {
     45  1.1    nonaka 
     46  1.1    nonaka 	/*
     47  1.1    nonaka 	 * cmd is used to specify a passthrough command to a controller or
     48  1.1    nonaka 	 *  namespace.
     49  1.1    nonaka 	 *
     50  1.1    nonaka 	 * The following fields from cmd may be specified by the caller:
     51  1.1    nonaka 	 *	* opcode
     52  1.1    nonaka 	 *	* nsid (namespace id) - for admin commands only
     53  1.1    nonaka 	 *	* cdw10-cdw15
     54  1.1    nonaka 	 *
     55  1.1    nonaka 	 * Remaining fields must be set to 0 by the caller.
     56  1.1    nonaka 	 */
     57  1.1    nonaka 	struct nvme_sqe		cmd;
     58  1.1    nonaka 
     59  1.1    nonaka 	/*
     60  1.1    nonaka 	 * cpl returns completion status for the passthrough command
     61  1.1    nonaka 	 *  specified by cmd.
     62  1.1    nonaka 	 *
     63  1.1    nonaka 	 * The following fields will be filled out by the driver, for
     64  1.1    nonaka 	 *  consumption by the caller:
     65  1.1    nonaka 	 *	* cdw0
     66  1.1    nonaka 	 *	* flags (except for phase)
     67  1.1    nonaka 	 *
     68  1.1    nonaka 	 * Remaining fields will be set to 0 by the driver.
     69  1.1    nonaka 	 */
     70  1.1    nonaka 	struct nvme_cqe		cpl;
     71  1.1    nonaka 
     72  1.1    nonaka 	/* buf is the data buffer associated with this passthrough command. */
     73  1.1    nonaka 	void			*buf;
     74  1.1    nonaka 
     75  1.1    nonaka 	/*
     76  1.1    nonaka 	 * len is the length of the data buffer associated with this
     77  1.1    nonaka 	 *  passthrough command.
     78  1.1    nonaka 	 */
     79  1.1    nonaka 	uint32_t		len;
     80  1.1    nonaka 
     81  1.1    nonaka 	/*
     82  1.1    nonaka 	 * is_read = 1 if the passthrough command will read data into the
     83  1.1    nonaka 	 *  supplied buffer from the controller.
     84  1.1    nonaka 	 *
     85  1.1    nonaka 	 * is_read = 0 if the passthrough command will write data from the
     86  1.1    nonaka 	 *  supplied buffer to the controller.
     87  1.1    nonaka 	 */
     88  1.1    nonaka 	uint32_t		is_read;
     89  1.1    nonaka 
     90  1.1    nonaka 	/*
     91  1.1    nonaka 	 * timeout (unit: ms)
     92  1.1    nonaka 	 *
     93  1.1    nonaka 	 * 0: use default timeout value
     94  1.1    nonaka 	 */
     95  1.1    nonaka 	uint32_t		timeout;
     96  1.1    nonaka };
     97  1.1    nonaka 
     98  1.4   msaitoh /* Endianness conversion functions for NVMe structs */
     99  1.3  christos static __inline void
    100  1.2    nonaka nvme_le128toh(uint64_t v[2])
    101  1.2    nonaka {
    102  1.2    nonaka #if _BYTE_ORDER != _LITTLE_ENDIAN
    103  1.2    nonaka 	uint64_t t;
    104  1.2    nonaka 
    105  1.2    nonaka 	t = le64toh(v[0]);
    106  1.2    nonaka 	v[0] = le64toh(v[1]);
    107  1.2    nonaka 	v[1] = t;
    108  1.2    nonaka #endif
    109  1.2    nonaka }
    110  1.2    nonaka 
    111  1.3  christos static __inline void
    112  1.2    nonaka nvme_namespace_format_swapbytes(struct nvm_namespace_format *format)
    113  1.2    nonaka {
    114  1.2    nonaka 
    115  1.2    nonaka #if _BYTE_ORDER != _LITTLE_ENDIAN
    116  1.2    nonaka 	format->ms = le16toh(format->ms);
    117  1.2    nonaka #endif
    118  1.2    nonaka }
    119  1.2    nonaka 
    120  1.3  christos static __inline void
    121  1.2    nonaka nvme_identify_namespace_swapbytes(struct nvm_identify_namespace *identify)
    122  1.2    nonaka {
    123  1.2    nonaka #if _BYTE_ORDER != _LITTLE_ENDIAN
    124  1.2    nonaka 	u_int i;
    125  1.2    nonaka 
    126  1.2    nonaka 	identify->nsze = le64toh(identify->nsze);
    127  1.2    nonaka 	identify->ncap = le64toh(identify->ncap);
    128  1.2    nonaka 	identify->nuse = le64toh(identify->nuse);
    129  1.2    nonaka 	identify->nawun = le16toh(identify->nawun);
    130  1.2    nonaka 	identify->nawupf = le16toh(identify->nawupf);
    131  1.2    nonaka 	identify->nacwu = le16toh(identify->nacwu);
    132  1.2    nonaka 	identify->nabsn = le16toh(identify->nabsn);
    133  1.2    nonaka 	identify->nabo = le16toh(identify->nabo);
    134  1.2    nonaka 	identify->nabspf = le16toh(identify->nabspf);
    135  1.2    nonaka 	identify->noiob = le16toh(identify->noiob);
    136  1.2    nonaka 	for (i = 0; i < __arraycount(identify->lbaf); i++)
    137  1.2    nonaka 		nvme_namespace_format_swapbytes(&identify->lbaf[i]);
    138  1.2    nonaka #endif
    139  1.2    nonaka }
    140  1.2    nonaka 
    141  1.3  christos static __inline void
    142  1.2    nonaka nvme_identify_psd_swapbytes(struct nvm_identify_psd *psd)
    143  1.2    nonaka {
    144  1.2    nonaka 
    145  1.2    nonaka #if _BYTE_ORDER != _LITTLE_ENDIAN
    146  1.2    nonaka 	psd->mp = le16toh(psd->mp);
    147  1.2    nonaka 	psd->enlat = le32toh(psd->enlat);
    148  1.2    nonaka 	psd->exlat = le32toh(psd->exlat);
    149  1.2    nonaka 	psd->idlp = le16toh(psd->idlp);
    150  1.2    nonaka 	psd->actp = le16toh(psd->actp);
    151  1.2    nonaka 	psd->ap = le16toh(psd->ap);
    152  1.2    nonaka #endif
    153  1.2    nonaka }
    154  1.2    nonaka 
    155  1.3  christos static __inline void
    156  1.2    nonaka nvme_identify_controller_swapbytes(struct nvm_identify_controller *identify)
    157  1.2    nonaka {
    158  1.2    nonaka #if _BYTE_ORDER != _LITTLE_ENDIAN
    159  1.2    nonaka 	u_int i;
    160  1.2    nonaka 
    161  1.2    nonaka 	identify->vid = le16toh(identify->vid);
    162  1.2    nonaka 	identify->ssvid = le16toh(identify->ssvid);
    163  1.2    nonaka 	identify->cntlid = le16toh(identify->cntlid);
    164  1.2    nonaka 	identify->ver = le32toh(identify->ver);
    165  1.2    nonaka 	identify->rtd3r = le32toh(identify->rtd3r);
    166  1.2    nonaka 	identify->rtd3e = le32toh(identify->rtd3e);
    167  1.2    nonaka 	identify->oaes = le32toh(identify->oaes);
    168  1.2    nonaka 	identify->ctrattr = le32toh(identify->ctrattr);
    169  1.2    nonaka 	identify->oacs = le16toh(identify->oacs);
    170  1.2    nonaka 	identify->wctemp = le16toh(identify->wctemp);
    171  1.2    nonaka 	identify->cctemp = le16toh(identify->cctemp);
    172  1.2    nonaka 	identify->mtfa = le16toh(identify->mtfa);
    173  1.2    nonaka 	identify->hmpre = le32toh(identify->hmpre);
    174  1.2    nonaka 	identify->hmmin = le32toh(identify->hmmin);
    175  1.2    nonaka 	nvme_le128toh(identify->untncap.tnvmcap);
    176  1.2    nonaka 	nvme_le128toh(identify->untncap.unvmcap);
    177  1.2    nonaka 	identify->rpmbs = le32toh(identify->rpmbs);
    178  1.2    nonaka 	identify->edstt = le16toh(identify->edstt);
    179  1.2    nonaka 	identify->kas = le16toh(identify->kas);
    180  1.2    nonaka 	identify->hctma = le16toh(identify->hctma);
    181  1.2    nonaka 	identify->mntmt = le16toh(identify->mntmt);
    182  1.2    nonaka 	identify->mxtmt = le16toh(identify->mxtmt);
    183  1.2    nonaka 	identify->sanicap = le32toh(identify->sanicap);
    184  1.2    nonaka 	identify->maxcmd = le16toh(identify->maxcmd);
    185  1.2    nonaka 	identify->nn = le32toh(identify->nn);
    186  1.2    nonaka 	identify->oncs = le16toh(identify->oncs);
    187  1.2    nonaka 	identify->fuses = le16toh(identify->fuses);
    188  1.2    nonaka 	identify->awun = le16toh(identify->awun);
    189  1.2    nonaka 	identify->awupf = le16toh(identify->awupf);
    190  1.2    nonaka 	identify->acwu = le16toh(identify->acwu);
    191  1.2    nonaka 	identify->sgls = le32toh(identify->sgls);
    192  1.2    nonaka 	for (i = 0; i < __arraycount(identify->psd); i++)
    193  1.2    nonaka 		nvme_identify_psd_swapbytes(&identify->psd[i]);
    194  1.2    nonaka #endif
    195  1.2    nonaka }
    196  1.2    nonaka 
    197  1.1    nonaka #endif /* __NVMEIO_H__ */
    198