nvme.c revision 0d140ff944190d3ff2fdba65e9864f3c03248162
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * This file and its contents are supplied under the terms of the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Common Development and Distribution License ("CDDL"), version 1.0.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * You may only use this file in accordance with the terms of version
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * 1.0 of the CDDL.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * A full copy of the text of the CDDL should have accompanied this
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * source. A copy of the CDDL is also available via the Internet at
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * http://www.illumos.org/license/CDDL.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * blkdev driver for NVMe compliant storage devices
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * This driver was written to conform to version 1.0e of the NVMe specification.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * It may work with newer versions, but that is completely untested and disabled
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * by default.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The driver has only been tested on x86 systems and will not work on big-
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * endian systems without changes to the code accessing registers and data
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * structures used by the hardware.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Interrupt Usage:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The driver will use a FIXED interrupt while configuring the device as the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * specification requires. Later in the attach process it will switch to MSI-X
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * or MSI if supported. The driver wants to have one interrupt vector per CPU,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * but it will work correctly if less are available. Interrupts can be shared
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * by queues, the interrupt handler will iterate through the I/O queue array by
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * steps of n_intr_cnt. Usually only the admin queue will share an interrupt
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * with one I/O queue. The interrupt handler will retrieve completed commands
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * from all queues sharing an interrupt vector and will post them to a taskq
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * for completion processing.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Command Processing:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * NVMe devices can have up to 65536 I/O queue pairs, with each queue holding up
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * to 65536 I/O commands. The driver will configure one I/O queue pair per
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * available interrupt vector, with the queue length usually much smaller than
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * the maximum of 65536. If the hardware doesn't provide enough queues, fewer
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * interrupt vectors will be used.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Additionally the hardware provides a single special admin queue pair that can
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * hold up to 4096 admin commands.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * From the hardware perspective both queues of a queue pair are independent,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * but they share some driver state: the command array (holding pointers to
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * commands currently being processed by the hardware) and the active command
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * counter. Access to the submission side of a queue pair and the shared state
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * is protected by nq_mutex. The completion side of a queue pair does not need
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * that protection apart from its access to the shared state; it is called only
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * in the interrupt handler which does not run concurrently for the same
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * interrupt vector.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * When a command is submitted to a queue pair the active command counter is
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * incremented and a pointer to the command is stored in the command array. The
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * array index is used as command identifier (CID) in the submission queue
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * entry. Some commands may take a very long time to complete, and if the queue
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * wraps around in that time a submission may find the next array slot to still
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * be used by a long-running command. In this case the array is sequentially
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * searched for the next free slot. The length of the command array is the same
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * as the configured queue length.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Namespace Support:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * NVMe devices can have multiple namespaces, each being a independent data
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * store. The driver supports multiple namespaces and creates a blkdev interface
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * for each namespace found. Namespaces can have various attributes to support
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * thin provisioning, extended LBAs, and protection information. This driver
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * does not support any of this and ignores namespaces that have these
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * attributes.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Blkdev Interface:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * This driver uses blkdev to do all the heavy lifting involved with presenting
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * a disk device to the system. As a result, the processing of I/O requests is
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * relatively simple as blkdev takes care of partitioning, boundary checks, DMA
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * setup, and splitting of transfers into manageable chunks.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * I/O requests coming in from blkdev are turned into NVM commands and posted to
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * an I/O queue. The queue is selected by taking the CPU id modulo the number of
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * queues. There is currently no timeout handling of I/O commands.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Blkdev also supports querying device/media information and generating a
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * devid. The driver reports the best block size as determined by the namespace
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * format back to blkdev as physical block size to support partition and block
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * alignment. The devid is composed using the device vendor ID, model number,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * serial number, and the namespace ID.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Error Handling:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Error handling is currently limited to detecting fatal hardware errors,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * either by asynchronous events, or synchronously through command status or
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * admin command timeouts. In case of severe errors the device is fenced off,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * all further requests will return EIO. FMA is then called to fault the device.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The hardware has a limit for outstanding asynchronous event requests. Before
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * this limit is known the driver assumes it is at least 1 and posts a single
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * asynchronous request. Later when the limit is known more asynchronous event
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * requests are posted to allow quicker reception of error information. When an
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * asynchronous event is posted by the hardware the driver will parse the error
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * status fields and log information or fault the device, depending on the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * severity of the asynchronous event. The asynchronous event request is then
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * reused and posted to the admin queue again.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * On command completion the command status is checked for errors. In case of
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * errors indicating a driver bug the driver panics. Almost all other error
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * status values just cause EIO to be returned.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Command timeouts are currently detected for all admin commands except
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * asynchronous event requests. If a command times out and the hardware appears
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * to be healthy the driver attempts to abort the command. If this fails the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * driver assumes the device to be dead, fences it off, and calls FMA to retire
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * it. In general admin commands are issued at attach time only. No timeout
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * handling of normal I/O commands is presently done.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * In some cases it may be possible that the ABORT command times out, too. In
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * that case the device is also declared dead and fenced off.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Quiesce / Fast Reboot:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The driver currently does not support fast reboot. A quiesce(9E) entry point
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * is still provided which is used to send a shutdown notification to the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * device.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Driver Configuration:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The following driver properties can be changed to control some aspects of the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * drivers operation:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - strict-version: can be set to 0 to allow devices conforming to newer
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * versions to be used
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - ignore-unknown-vendor-status: can be set to 1 to not handle any vendor
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * specific command status as a fatal error leading device faulting
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - admin-queue-len: the maximum length of the admin queue (16-4096)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - io-queue-len: the maximum length of the I/O queues (16-65536)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - async-event-limit: the maximum number of asynchronous event requests to be
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * posted by the driver
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * TODO:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - figure out sane default for I/O queue depth reported to blkdev
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - polled I/O support to support kernel core dumping
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - FMA handling of media errors
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for the Volatile Write Cache
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for devices supporting very large I/O requests using chained PRPs
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for querying log pages from user space
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for configuring hardware parameters like interrupt coalescing
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for media formatting and hard partitioning into namespaces
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for big-endian systems
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - support for fast reboot
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/byteorder.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#ifdef _BIG_ENDIAN
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#error nvme driver needs porting for big-endian platforms
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#endif
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/modctl.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/conf.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/devops.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/ddi.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/sunddi.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/bitmap.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/sysmacros.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/param.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/varargs.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/cpuvar.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/disp.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/blkdev.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/atomic.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include <sys/archsystm.h>
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld#include <sys/sata/sata_hba.h>
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include "nvme_reg.h"
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld#include "nvme_var.h"
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/* NVMe spec version supported */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic const int nvme_version_major = 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic const int nvme_version_minor = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_attach(dev_info_t *, ddi_attach_cmd_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_detach(dev_info_t *, ddi_detach_cmd_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_quiesce(dev_info_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_fm_errcb(dev_info_t *, ddi_fm_error_t *, const void *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_setup_interrupts(nvme_t *, int, int);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_release_interrupts(nvme_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic uint_t nvme_intr(caddr_t, caddr_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_shutdown(nvme_t *, int, boolean_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t nvme_reset(nvme_t *, boolean_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_init(nvme_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic nvme_cmd_t *nvme_alloc_cmd(nvme_t *, int);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_free_cmd(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic nvme_cmd_t *nvme_create_nvm_cmd(nvme_namespace_t *, uint8_t,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_xfer_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_admin_cmd(nvme_cmd_t *, int);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_submit_cmd(nvme_qpair_t *, nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic nvme_cmd_t *nvme_retrieve_cmd(nvme_t *, nvme_qpair_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t nvme_wait_cmd(nvme_cmd_t *, uint_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_wakeup_cmd(void *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_async_event_task(void *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_check_unknown_cmd_status(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_check_vendor_cmd_status(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_check_integrity_cmd_status(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_check_specific_cmd_status(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_check_generic_cmd_status(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline int nvme_check_cmd_status(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_abort_cmd(nvme_cmd_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_async_event(nvme_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void *nvme_get_logpage(nvme_t *, uint8_t, ...);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void *nvme_identify(nvme_t *, uint32_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_set_nqueues(nvme_t *, uint16_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_free_dma(nvme_dma_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_zalloc_dma(nvme_t *, size_t, uint_t, ddi_dma_attr_t *,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_dma_t **);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_zalloc_queue_dma(nvme_t *, uint32_t, uint16_t, uint_t,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_dma_t **);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_free_qpair(nvme_qpair_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_alloc_qpair(nvme_t *, uint32_t, nvme_qpair_t **, int);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_create_io_qpair(nvme_t *, nvme_qpair_t *, uint16_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline void nvme_put64(nvme_t *, uintptr_t, uint64_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline void nvme_put32(nvme_t *, uintptr_t, uint32_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline uint64_t nvme_get64(nvme_t *, uintptr_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline uint32_t nvme_get32(nvme_t *, uintptr_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t nvme_check_regs_hdl(nvme_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t nvme_check_dma_hdl(nvme_dma_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_fill_prp(nvme_cmd_t *, bd_xfer_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_bd_xfer_done(void *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_bd_driveinfo(void *, bd_drive_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_bd_mediainfo(void *, bd_media_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_bd_cmd(nvme_namespace_t *, bd_xfer_t *, uint8_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_bd_read(void *, bd_xfer_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_bd_write(void *, bd_xfer_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_bd_sync(void *, bd_xfer_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int nvme_bd_devid(void *, dev_info_t *, ddi_devid_t *);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void nvme_prepare_devid(nvme_t *, uint32_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void *nvme_state;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic kmem_cache_t *nvme_cmd_cache;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * DMA attributes for queue DMA memory
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Queue DMA memory must be page aligned. The maximum length of a queue is
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * 65536 entries, and an entry can be 64 bytes long.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic ddi_dma_attr_t nvme_queue_dma_attr = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_version = DMA_ATTR_V0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_addr_lo = 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_addr_hi = 0xffffffffffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_count_max = (UINT16_MAX + 1) * sizeof (nvme_sqe_t),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_align = 0x1000,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_burstsizes = 0x7ff,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_minxfer = 0x1000,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_maxxfer = (UINT16_MAX + 1) * sizeof (nvme_sqe_t),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_seg = 0xffffffffffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_sgllen = 1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_granular = 1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_flags = 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * DMA attributes for transfers using Physical Region Page (PRP) entries
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * A PRP entry describes one page of DMA memory using the page size specified
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * in the controller configuration's memory page size register (CC.MPS). It uses
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * a 64bit base address aligned to this page size. There is no limitation on
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * chaining PRPs together for arbitrarily large DMA transfers.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic ddi_dma_attr_t nvme_prp_dma_attr = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_version = DMA_ATTR_V0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_addr_lo = 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_addr_hi = 0xffffffffffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_count_max = 0xfff,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_align = 0x1000,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_burstsizes = 0x7ff,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_minxfer = 0x1000,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_maxxfer = 0x1000,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_seg = 0xffffffffffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_sgllen = -1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_granular = 1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_flags = 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * DMA attributes for transfers using scatter/gather lists
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * A SGL entry describes a chunk of DMA memory using a 64bit base address and a
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * 32bit length field. SGL Segment and SGL Last Segment entries require the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * length to be a multiple of 16 bytes.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic ddi_dma_attr_t nvme_sgl_dma_attr = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_version = DMA_ATTR_V0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_addr_lo = 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_addr_hi = 0xffffffffffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_count_max = 0xffffffffUL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_align = 1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_burstsizes = 0x7ff,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_minxfer = 0x10,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_maxxfer = 0xfffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_seg = 0xffffffffffffffffULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_sgllen = -1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_granular = 0x10,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .dma_attr_flags = 0
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic ddi_device_acc_attr_t nvme_reg_acc_attr = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devacc_attr_version = DDI_DEVICE_ATTR_V0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devacc_attr_dataorder = DDI_STRICTORDER_ACC
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic struct dev_ops nvme_dev_ops = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_rev = DEVO_REV,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_refcnt = 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_getinfo = ddi_no_info,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_identify = nulldev,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_probe = nulldev,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_attach = nvme_attach,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_detach = nvme_detach,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_reset = nodev,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_cb_ops = NULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_bus_ops = NULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_power = NULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .devo_quiesce = nvme_quiesce,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic struct modldrv nvme_modldrv = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .drv_modops = &mod_driverops,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .drv_linkinfo = "NVMe v1.0e",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .drv_dev_ops = &nvme_dev_ops
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic struct modlinkage nvme_modlinkage = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .ml_rev = MODREV_1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .ml_linkage = { &nvme_modldrv, NULL }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic bd_ops_t nvme_bd_ops = {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_version = BD_OPS_VERSION_0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_drive_info = nvme_bd_driveinfo,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_media_info = nvme_bd_mediainfo,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_devid_init = nvme_bd_devid,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_sync_cache = nvme_bd_sync,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_read = nvme_bd_read,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld .o_write = nvme_bd_write,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld};
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldint
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld_init(void)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int error;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld error = ddi_soft_state_init(&nvme_state, sizeof (nvme_t), 1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (error != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (error);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_cache = kmem_cache_create("nvme_cmd_cache",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme_cmd_t), 64, NULL, NULL, NULL, NULL, NULL, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_mod_init(&nvme_dev_ops);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld error = mod_install(&nvme_modlinkage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (error != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_soft_state_fini(&nvme_state);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_mod_fini(&nvme_dev_ops);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (error);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldint
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld_fini(void)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int error;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld error = mod_remove(&nvme_modlinkage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (error == DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_soft_state_fini(&nvme_state);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_cache_destroy(nvme_cmd_cache);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_mod_fini(&nvme_dev_ops);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (error);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldint
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld_info(struct modinfo *modinfop)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (mod_info(&nvme_modlinkage, modinfop));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_put64(nvme_t *nvme, uintptr_t reg, uint64_t val)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(((uintptr_t)(nvme->n_regs + reg) & 0x7) == 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_put64(nvme->n_regh, (uint64_t *)(nvme->n_regs + reg), val);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_put32(nvme_t *nvme, uintptr_t reg, uint32_t val)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(((uintptr_t)(nvme->n_regs + reg) & 0x3) == 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_put32(nvme->n_regh, (uint32_t *)(nvme->n_regs + reg), val);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline uint64_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_get64(nvme_t *nvme, uintptr_t reg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld uint64_t val;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(((uintptr_t)(nvme->n_regs + reg) & 0x7) == 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld val = ddi_get64(nvme->n_regh, (uint64_t *)(nvme->n_regs + reg));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (val);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline uint32_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_get32(nvme_t *nvme, uintptr_t reg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld uint32_t val;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(((uintptr_t)(nvme->n_regs + reg) & 0x3) == 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld val = ddi_get32(nvme->n_regh, (uint32_t *)(nvme->n_regs + reg));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (val);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_regs_hdl(nvme_t *nvme)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_error_t error;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_acc_err_get(nvme->n_regh, &error, DDI_FME_VERSION);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (error.fme_status != DDI_FM_OK)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_TRUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_dma_hdl(nvme_dma_t *dma)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_error_t error;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (dma == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_dma_err_get(dma->nd_dmah, &error, DDI_FME_VERSION);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (error.fme_status != DDI_FM_OK)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_TRUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_free_dma(nvme_dma_t *dma)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (dma->nd_dmah != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_dma_unbind_handle(dma->nd_dmah);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (dma->nd_acch != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_mem_free(&dma->nd_acch);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (dma->nd_dmah != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_free_handle(&dma->nd_dmah);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(dma, sizeof (nvme_dma_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_zalloc_dma(nvme_t *nvme, size_t len, uint_t flags,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_attr_t *dma_attr, nvme_dma_t **ret)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_dma_t *dma = kmem_zalloc(sizeof (nvme_dma_t), KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ddi_dma_alloc_handle(nvme->n_dip, dma_attr, DDI_DMA_SLEEP, NULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &dma->nd_dmah) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Due to DDI_DMA_SLEEP this can't be DDI_DMA_NORESOURCES, and
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * the only other possible error is DDI_DMA_BADATTR which
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * indicates a driver bug which should cause a panic.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_PANIC,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to get DMA handle, check DMA attributes");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * ddi_dma_mem_alloc() can only fail when DDI_DMA_NOSLEEP is specified
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * or the flags are conflicting, which isn't the case here.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_dma_mem_alloc(dma->nd_dmah, len, &nvme->n_reg_acc_attr,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &dma->nd_memp,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &dma->nd_len, &dma->nd_acch);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ddi_dma_addr_bind_handle(dma->nd_dmah, NULL, dma->nd_memp,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dma->nd_len, flags | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &dma->nd_cookie, &dma->nd_ncookie) != DDI_DMA_MAPPED) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to bind DMA memory");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_dma_bind_err);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *ret = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_dma(dma);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bzero(dma->nd_memp, dma->nd_len);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *ret = dma;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_zalloc_queue_dma(nvme_t *nvme, uint32_t nentry, uint16_t qe_len,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld uint_t flags, nvme_dma_t **dma)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld uint32_t len = nentry * qe_len;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_attr_t q_dma_attr = nvme->n_queue_dma_attr;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld len = roundup(len, nvme->n_pagesize);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld q_dma_attr.dma_attr_minxfer = len;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_zalloc_dma(nvme, len, flags, &q_dma_attr, dma)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to get DMA memory for queue");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if ((*dma)->nd_ncookie != 1) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!got too many cookies for queue DMA");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (*dma) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_dma(*dma);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *dma = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_free_qpair(nvme_qpair_t *qp)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_destroy(&qp->nq_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_sqdma != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_dma(qp->nq_sqdma);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_cqdma != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_dma(qp->nq_cqdma);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_active_cmds > 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != qp->nq_nentry; i++)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_cmd[i] != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(qp->nq_cmd[i]);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_cmd != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(qp->nq_cmd, sizeof (nvme_cmd_t *) * qp->nq_nentry);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(qp, sizeof (nvme_qpair_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_alloc_qpair(nvme_t *nvme, uint32_t nentry, nvme_qpair_t **nqp,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int idx)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_qpair_t *qp = kmem_zalloc(sizeof (*qp), KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_init(&qp->nq_mutex, NULL, MUTEX_DRIVER,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_INTR_PRI(nvme->n_intr_pri));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_zalloc_queue_dma(nvme, nentry, sizeof (nvme_sqe_t),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_DMA_WRITE, &qp->nq_sqdma) != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_zalloc_queue_dma(nvme, nentry, sizeof (nvme_cqe_t),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_DMA_READ, &qp->nq_cqdma) != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_sq = (nvme_sqe_t *)qp->nq_sqdma->nd_memp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_cq = (nvme_cqe_t *)qp->nq_cqdma->nd_memp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_nentry = nentry;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_sqtdbl = NVME_REG_SQTDBL(nvme, idx);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_cqhdbl = NVME_REG_CQHDBL(nvme, idx);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_cmd = kmem_zalloc(sizeof (nvme_cmd_t *) * nentry, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_next_cmd = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *nqp = qp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_qpair(qp);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *nqp = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic nvme_cmd_t *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_alloc_cmd(nvme_t *nvme, int kmflag)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = kmem_cache_alloc(nvme_cmd_cache, kmflag);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bzero(cmd, sizeof (nvme_cmd_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_nvme = nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_init(&cmd->nc_mutex, NULL, MUTEX_DRIVER,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_INTR_PRI(nvme->n_intr_pri));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cv_init(&cmd->nc_cv, NULL, CV_DRIVER, NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_free_cmd(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_dma) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_dma(cmd->nc_dma);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_dma = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cv_destroy(&cmd->nc_cv);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_destroy(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_cache_free(nvme_cmd_cache, cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_submit_cmd(nvme_qpair_t *qp, nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_sqtdbl_t tail = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_enter(&qp->nq_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_active_cmds == qp->nq_nentry) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&qp->nq_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_completed = B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Try to insert the cmd into the active cmd array at the nq_next_cmd
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * slot. If the slot is already occupied advance to the next slot and
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * try again. This can happen for long running commands like async event
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * requests.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld while (qp->nq_cmd[qp->nq_next_cmd] != NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_next_cmd = (qp->nq_next_cmd + 1) % qp->nq_nentry;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_cmd[qp->nq_next_cmd] = cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_active_cmds++;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cid = qp->nq_next_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bcopy(&cmd->nc_sqe, &qp->nq_sq[qp->nq_sqtail], sizeof (nvme_sqe_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_dma_sync(qp->nq_sqdma->nd_dmah,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme_sqe_t) * qp->nq_sqtail,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme_sqe_t), DDI_DMA_SYNC_FORDEV);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_next_cmd = (qp->nq_next_cmd + 1) % qp->nq_nentry;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld tail.b.sqtdbl_sqt = qp->nq_sqtail = (qp->nq_sqtail + 1) % qp->nq_nentry;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(cmd->nc_nvme, qp->nq_sqtdbl, tail.r);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&qp->nq_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic nvme_cmd_t *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_retrieve_cmd(nvme_t *nvme, nvme_qpair_t *qp)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_cqhdbl_t head = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_dma_sync(qp->nq_cqdma->nd_dmah, 0,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme_cqe_t) * qp->nq_nentry, DDI_DMA_SYNC_FORKERNEL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cqe = &qp->nq_cq[qp->nq_cqhead];
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Check phase tag of CQE. Hardware inverts it for new entries. */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cqe->cqe_sf.sf_p == qp->nq_phase)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(nvme->n_ioq[cqe->cqe_sqid] == qp);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cqe->cqe_cid < qp->nq_nentry);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_enter(&qp->nq_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd = qp->nq_cmd[cqe->cqe_cid];
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_cmd[cqe->cqe_cid] = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_active_cmds--;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&qp->nq_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd != NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_nvme == nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqid == cqe->cqe_sqid);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_cid == cqe->cqe_cid);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bcopy(cqe, &cmd->nc_cqe, sizeof (nvme_cqe_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_sqhead = cqe->cqe_sqhd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld head.b.cqhdbl_cqh = qp->nq_cqhead = (qp->nq_cqhead + 1) % qp->nq_nentry;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Toggle phase on wrap-around. */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (qp->nq_cqhead == 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qp->nq_phase = qp->nq_phase ? 0 : 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(cmd->nc_nvme, qp->nq_cqhdbl, head.r);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_unknown_cmd_status(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe = &cmd->nc_cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!unknown command status received: opc = %x, sqid = %d, cid = %d, "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "sc = %x, sct = %x, dnr = %d, m = %d", cmd->nc_sqe.sqe_opc,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cqe->cqe_sqid, cqe->cqe_cid, cqe->cqe_sf.sf_sc, cqe->cqe_sf.sf_sct,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cqe->cqe_sf.sf_dnr, cqe->cqe_sf.sf_m);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_ILLRQ);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_nvme->n_strict_version) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(cmd->nc_nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_vendor_cmd_status(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe = &cmd->nc_cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!unknown command status received: opc = %x, sqid = %d, cid = %d, "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "sc = %x, sct = %x, dnr = %d, m = %d", cmd->nc_sqe.sqe_opc,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cqe->cqe_sqid, cqe->cqe_cid, cqe->cqe_sf.sf_sc, cqe->cqe_sf.sf_sct,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cqe->cqe_sf.sf_dnr, cqe->cqe_sf.sf_m);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_nvme->n_ignore_unknown_vendor_status) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(cmd->nc_nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_integrity_cmd_status(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe = &cmd->nc_cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (cqe->cqe_sf.sf_sc) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_INT_NVM_WRITE:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* write fail */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: post ereport */
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_MEDIA);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_INT_NVM_READ:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* read fail */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: post ereport */
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_MEDIA);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld default:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_unknown_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_generic_cmd_status(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe = &cmd->nc_cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (cqe->cqe_sf.sf_sc) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_SUCCESS:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Errors indicating a bug in the driver should cause a panic.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_INV_OPC:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Command Opcode */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "invalid opcode in cmd %p", (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_INV_FLD:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Field in Command */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "invalid field in cmd %p", (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_ID_CNFL:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Command ID Conflict */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "cmd ID conflict in cmd %p", (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_INV_NS:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Namespace or Format */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "invalid NS/format in cmd %p", (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_NVM_LBA_RANGE:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* LBA Out Of Range */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "LBA out of range in cmd %p", (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Non-fatal errors, handle gracefully.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_DATA_XFR_ERR:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Data Transfer Error (DMA) */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: post ereport */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_data_xfr_err);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_NTRDY);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_INTERNAL_ERR:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Internal Error. The spec (v1.0, section 4.5.1.2) says
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * detailed error information is returned as async event,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * so we pretty much ignore the error here and handle it
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * in the async event handler.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_internal_err);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_NTRDY);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_ABORT_REQUEST:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Command Abort Requested. This normally happens only when a
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * command times out.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: post ereport or change blkdev to handle this? */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_abort_rq_err);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ECANCELED);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_ABORT_PWRLOSS:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Command Aborted due to Power Loss Notification */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(cmd->nc_nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_ABORT_SQ_DEL:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Command Aborted due to SQ Deletion */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_abort_sq_del);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_NVM_CAP_EXC:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Capacity Exceeded */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_nvm_cap_exc);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_MEDIA);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_GEN_NVM_NS_NOTRDY:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Namespace Not Ready */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_nvm_ns_notrdy);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_NTRDY);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld default:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_unknown_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_specific_cmd_status(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe = &cmd->nc_cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (cqe->cqe_sf.sf_sc) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_INV_CQ:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Completion Queue Invalid */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_CREATE_SQUEUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_cq_err);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_INV_QID:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Queue Identifier */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_CREATE_SQUEUE ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_DELETE_SQUEUE ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_CREATE_CQUEUE ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_DELETE_CQUEUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_qid_err);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_MAX_QSZ_EXC:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Max Queue Size Exceeded */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_CREATE_SQUEUE ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_CREATE_CQUEUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_max_qsz_exc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_ABRT_CMD_EXC:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Abort Command Limit Exceeded */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_ABORT);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "abort command limit exceeded in cmd %p", (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_ASYNC_EVREQ_EXC:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Async Event Request Limit Exceeded */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_ASYNC_EVENT);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "async event request limit exceeded in cmd %p",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void *)cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_INV_INT_VECT:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Interrupt Vector */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_CREATE_CQUEUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_int_vect);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_INV_LOG_PAGE:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Log Page */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_GET_LOG_PAGE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_log_page);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_ILLRQ);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_INV_FORMAT:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Format */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_FORMAT);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_format);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_ILLRQ);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_INV_Q_DEL:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Queue Deletion */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_DELETE_CQUEUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_q_del);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_NVM_CNFL_ATTR:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Conflicting Attributes */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_DSET_MGMT ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_READ ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_WRITE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_cnfl_attr);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_ILLRQ);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_NVM_INV_PROT:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Invalid Protection Information */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_COMPARE ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_READ ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_WRITE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_inv_prot);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_ILLRQ);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EINVAL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_CQE_SC_SPC_NVM_READONLY:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Write to Read Only Range */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqe.sqe_opc == NVME_OPC_NVM_WRITE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_readonly);
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld bd_error(cmd->nc_xfer, BD_ERR_ILLRQ);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EROFS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld default:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_unknown_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic inline int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_check_cmd_status(nvme_cmd_t *cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cqe_t *cqe = &cmd->nc_cqe;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* take a shortcut if everything is alright */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cqe->cqe_sf.sf_sct == NVME_CQE_SCT_GENERIC &&
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cqe->cqe_sf.sf_sc == NVME_CQE_SC_GEN_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cqe->cqe_sf.sf_sct == NVME_CQE_SCT_GENERIC)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_generic_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else if (cqe->cqe_sf.sf_sct == NVME_CQE_SCT_SPECIFIC)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_specific_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else if (cqe->cqe_sf.sf_sct == NVME_CQE_SCT_INTEGRITY)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_integrity_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else if (cqe->cqe_sf.sf_sct == NVME_CQE_SCT_VENDOR)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_vendor_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_check_unknown_cmd_status(cmd));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * nvme_abort_cmd_cb -- replaces nc_callback of aborted commands
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * This functions takes care of cleaning up aborted commands. The command
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * status is checked to catch any fatal errors.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_abort_cmd_cb(void *arg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Grab the command mutex. Once we have it we hold the last reference
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * to the command and can safely free it.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_enter(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) nvme_check_cmd_status(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_abort_cmd(nvme_cmd_t *abort_cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = abort_cmd->nc_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_abort_cmd_t ac = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_p(&nvme->n_abort_sema);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ac.b.ac_cid = abort_cmd->nc_sqe.sqe_cid;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ac.b.ac_sqid = abort_cmd->nc_sqid;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Drop the mutex of the aborted command. From this point on
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * we must assume that the abort callback has freed the command.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&abort_cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_ABORT;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_wakeup_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = ac.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Send the ABORT to the hardware. The ABORT command will return _after_
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * the aborted command has completed (aborted or otherwise).
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_admin_cmd(cmd, NVME_ADMIN_CMD_TIMEOUT) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_v(&nvme->n_abort_sema);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_admin_cmd failed for ABORT");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_abort_failed);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_v(&nvme->n_abort_sema);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!ABORT failed with sct = %x, sc = %x",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sct, cmd->nc_cqe.cqe_sf.sf_sc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_abort_failed);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld } else {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_cmd_aborted);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld/*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * nvme_wait_cmd -- wait for command completion or timeout
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Returns B_TRUE if the command completed normally.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Returns B_FALSE if the command timed out and an abort was attempted. The
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * command mutex will be dropped and the command must be considered freed. The
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * freeing of the command is normally done by the abort command callback.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * In case of a serious error or a timeout of the abort command the hardware
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * will be declared dead and FMA will be notified.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_wait_cmd(nvme_cmd_t *cmd, uint_t usec)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld clock_t timeout = ddi_get_lbolt() + drv_usectohz(usec);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = cmd->nc_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_csts_t csts;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(mutex_owned(&cmd->nc_mutex));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld while (!cmd->nc_completed) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cv_timedwait(&cmd->nc_cv, &cmd->nc_mutex, timeout) == -1)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_completed)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_TRUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The command timed out. Change the callback to the cleanup function.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_abort_cmd_cb;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Check controller for fatal status, any errors associated with the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * register or DMA handle, or for a double timeout (abort command timed
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * out). If necessary log a warning and call FMA.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!command timeout, "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "OPC = %x, CFS = %d", cmd->nc_sqe.sqe_opc, csts.b.csts_cfs);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_cmd_timeout);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_cfs ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_check_regs_hdl(nvme) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_check_dma_hdl(cmd->nc_dma) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc == NVME_OPC_ABORT) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld } else {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Try to abort the command. The command mutex is released by
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * nvme_abort_cmd().
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * If the abort succeeds it will have freed the aborted command.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * If the abort fails for other reasons we must assume that the
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * command may complete at any time, and the callback will free
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * it for us.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_abort_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_wakeup_cmd(void *arg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_enter(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * There is a slight chance that this command completed shortly after
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * the timeout was hit in nvme_wait_cmd() but before the callback was
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * changed. Catch that case here and clean up accordingly.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_callback == nvme_abort_cmd_cb) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_abort_cmd_cb(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_completed = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cv_signal(&cmd->nc_cv);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_async_event_task(void *arg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = cmd->nc_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_error_log_entry_t *error_log = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_health_log_t *health_log = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_async_event_t event;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int ret;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Check for errors associated with the async request itself. The only
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * command-specific error is "async event limit exceeded", which
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * indicates a programming error in the driver and causes a panic in
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * nvme_check_cmd_status().
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Other possible errors are various scenarios where the async request
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * was aborted, or internal errors in the device. Internal errors are
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * reported to FMA, the command aborts need no special handling here.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!async event request returned failure, sct = %x, "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "sc = %x, dnr = %d, m = %d", cmd->nc_cqe.cqe_sf.sf_sct,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sc, cmd->nc_cqe.cqe_sf.sf_dnr,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_m);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_cqe.cqe_sf.sf_sct == NVME_CQE_SCT_GENERIC &&
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sc == NVME_CQE_SC_GEN_INTERNAL_ERR) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(cmd->nc_nvme->n_dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld event.r = cmd->nc_cqe.cqe_dw0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Clear CQE and re-submit the async request. */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bzero(&cmd->nc_cqe, sizeof (nvme_cqe_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = nvme_submit_cmd(nvme->n_adminq, cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to resubmit async event request");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_async_resubmit_failed);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (event.b.ae_type) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_TYPE_ERROR:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (event.b.ae_logpage == NVME_LOGPAGE_ERROR) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld error_log = (nvme_error_log_entry_t *)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_get_logpage(nvme, event.b.ae_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld } else {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!wrong logpage in "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "async event reply: %d", event.b.ae_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_wrong_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (event.b.ae_info) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_ERROR_INV_SQ:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "invalid submission queue");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_ERROR_INV_DBL:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_PANIC, "programming error: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "invalid doorbell write value");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_ERROR_DIAGFAIL:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!diagnostic failure");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_diagfail_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_ERROR_PERSISTENT:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!persistent internal "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "device error");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_persistent_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_ERROR_TRANSIENT:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!transient internal "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "device error");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: send ereport */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_transient_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_ERROR_FW_LOAD:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!firmware image load error");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_fw_load_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_TYPE_HEALTH:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (event.b.ae_logpage == NVME_LOGPAGE_HEALTH) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld health_log = (nvme_health_log_t *)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_get_logpage(nvme, event.b.ae_logpage, -1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld } else {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!wrong logpage in "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "async event reply: %d", event.b.ae_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_wrong_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (event.b.ae_info) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_HEALTH_RELIABILITY:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!device reliability compromised");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: send ereport */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_reliability_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_HEALTH_TEMPERATURE:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!temperature above threshold");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: send ereport */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_temperature_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_HEALTH_SPARE:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!spare space below threshold");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: send ereport */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_spare_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_ASYNC_TYPE_VENDOR:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!vendor specific async event "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "received, info = %x, logpage = %x", event.b.ae_info,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld event.b.ae_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_vendor_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld default:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!unknown async event received, "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "type = %x, info = %x, logpage = %x", event.b.ae_type,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld event.b.ae_info, event.b.ae_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_unknown_event);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (error_log)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(error_log, sizeof (nvme_error_log_entry_t) *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_error_log_len);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (health_log)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(health_log, sizeof (nvme_health_log_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_admin_cmd(nvme_cmd_t *cmd, int usec)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int ret;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_enter(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = nvme_submit_cmd(cmd->nc_nvme->n_adminq, cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(cmd->nc_nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_submit_cmd failed");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&cmd->nc_nvme->n_admin_queue_full);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_wait_cmd(cmd, usec) == B_FALSE) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The command timed out. An abort command was posted that
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * will take care of the cleanup.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld mutex_exit(&cmd->nc_mutex);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_async_event(nvme_t *nvme)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int ret;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_ASYNC_EVENT;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_async_event_task;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = nvme_submit_cmd(nvme->n_adminq, cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_submit_cmd failed for ASYNCHRONOUS EVENT");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_get_logpage(nvme_t *nvme, uint8_t logpage, ...)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld void *buf = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_getlogpage_t getlogpage;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld size_t bufsize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld va_list ap;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld va_start(ap, logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_wakeup_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_GET_LOG_PAGE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld getlogpage.b.lp_lid = logpage;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (logpage) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_LOGPAGE_ERROR:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_nsid = (uint32_t)-1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bufsize = nvme->n_error_log_len *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme_error_log_entry_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_LOGPAGE_HEALTH:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_nsid = va_arg(ap, uint32_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bufsize = sizeof (nvme_health_log_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_LOGPAGE_FWSLOT:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_nsid = (uint32_t)-1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bufsize = sizeof (nvme_fwslot_log_t);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld default:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!unknown log page requested: %d",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_unknown_logpage);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld va_end(ap);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
9898bbdb3aee66342820dd5570925a6ad4783d08Hans Rosenfeld getlogpage.b.lp_numd = bufsize / sizeof (uint32_t) - 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = getlogpage.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_zalloc_dma(nvme, getlogpage.b.lp_numd * sizeof (uint32_t),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_DMA_READ, &nvme->n_prp_dma_attr, &cmd->nc_dma) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_zalloc_dma failed for GET LOG PAGE");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_dma->nd_ncookie > 2) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!too many DMA cookies for GET LOG PAGE");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_too_many_cookies);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[0] = cmd->nc_dma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_dma->nd_ncookie > 1) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_nextcookie(cmd->nc_dma->nd_dmah,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &cmd->nc_dma->nd_cookie);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[1] =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_dma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_admin_cmd(cmd, NVME_ADMIN_CMD_TIMEOUT) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_admin_cmd failed for GET LOG PAGE");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!GET LOG PAGE failed with sct = %x, sc = %x",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sct, cmd->nc_cqe.cqe_sf.sf_sc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld buf = kmem_alloc(bufsize, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bcopy(cmd->nc_dma->nd_memp, buf, bufsize);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (buf);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_identify(nvme_t *nvme, uint32_t nsid)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld void *buf = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_wakeup_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_IDENTIFY;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_nsid = nsid;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = nsid ? NVME_IDENTIFY_NSID : NVME_IDENTIFY_CTRL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_zalloc_dma(nvme, NVME_IDENTIFY_BUFSIZE, DDI_DMA_READ,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &nvme->n_prp_dma_attr, &cmd->nc_dma) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_zalloc_dma failed for IDENTIFY");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_dma->nd_ncookie > 2) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!too many DMA cookies for IDENTIFY");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld atomic_inc_32(&nvme->n_too_many_cookies);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[0] = cmd->nc_dma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd->nc_dma->nd_ncookie > 1) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_nextcookie(cmd->nc_dma->nd_dmah,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &cmd->nc_dma->nd_cookie);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[1] =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_dma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_admin_cmd(cmd, NVME_ADMIN_CMD_TIMEOUT) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_admin_cmd failed for IDENTIFY");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!IDENTIFY failed with sct = %x, sc = %x",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sct, cmd->nc_cqe.cqe_sf.sf_sc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld buf = kmem_alloc(NVME_IDENTIFY_BUFSIZE, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bcopy(cmd->nc_dma->nd_memp, buf, NVME_IDENTIFY_BUFSIZE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (buf);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_set_nqueues(nvme_t *nvme, uint16_t nqueues)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_nqueue_t nq = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nq.b.nq_nsq = nq.b.nq_ncq = nqueues;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_wakeup_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_SET_FEATURES;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = NVME_FEAT_NQUEUES;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw11 = nq.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_admin_cmd(cmd, NVME_ADMIN_CMD_TIMEOUT) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_admin_cmd failed for SET FEATURES (NQUEUES)");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!SET FEATURES (NQUEUES) failed with sct = %x, sc = %x",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sct, cmd->nc_cqe.cqe_sf.sf_sc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nq.r = cmd->nc_cqe.cqe_dw0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Always use the same number of submission and completion queues, and
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * never use more than the requested number of queues.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (MIN(nqueues, MIN(nq.b.nq_nsq, nq.b.nq_ncq)));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_create_io_qpair(nvme_t *nvme, nvme_qpair_t *qp, uint16_t idx)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_create_queue_dw10_t dw10 = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_create_cq_dw11_t c_dw11 = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_create_sq_dw11_t s_dw11 = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dw10.b.q_qid = idx;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dw10.b.q_qsize = qp->nq_nentry - 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld c_dw11.b.cq_pc = 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld c_dw11.b.cq_ien = 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld c_dw11.b.cq_iv = idx % nvme->n_intr_cnt;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_wakeup_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_CREATE_CQUEUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = dw10.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw11 = c_dw11.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[0] = qp->nq_cqdma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_admin_cmd(cmd, NVME_ADMIN_CMD_TIMEOUT) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_admin_cmd failed for CREATE CQUEUE");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!CREATE CQUEUE failed with sct = %x, sc = %x",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sct, cmd->nc_cqe.cqe_sf.sf_sc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld s_dw11.b.sq_pc = 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld s_dw11.b.sq_cqid = idx;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd = nvme_alloc_cmd(nvme, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_wakeup_cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = NVME_OPC_CREATE_SQUEUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = dw10.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw11 = s_dw11.r;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[0] = qp->nq_sqdma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_admin_cmd(cmd, NVME_ADMIN_CMD_TIMEOUT) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!nvme_admin_cmd failed for CREATE SQUEUE");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_check_cmd_status(cmd)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!CREATE SQUEUE failed with sct = %x, sc = %x",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_cqe.cqe_sf.sf_sct, cmd->nc_cqe.cqe_sf.sf_sc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic boolean_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_reset(nvme_t *nvme, boolean_t quiesce)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_csts_t csts;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_CC, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_rdy == 1) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_CC, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != nvme->n_timeout * 10; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_rdy == 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (quiesce)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drv_usecwait(50000);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld delay(drv_usectohz(50000));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_AQA, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_ASQ, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_ACQ, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (csts.b.csts_rdy == 0 ? B_TRUE : B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_shutdown(nvme_t *nvme, int mode, boolean_t quiesce)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_cc_t cc;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_csts_t csts;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(mode == NVME_CC_SHN_NORMAL || mode == NVME_CC_SHN_ABRUPT);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.r = nvme_get32(nvme, NVME_REG_CC);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.b.cc_shn = mode & 0x3;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_CC, cc.r);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != 10; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_shst == NVME_CSTS_SHN_COMPLETE)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (quiesce)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drv_usecwait(100000);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld delay(drv_usectohz(100000));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_prepare_devid(nvme_t *nvme, uint32_t nsid)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld char model[sizeof (nvme->n_idctl->id_model) + 1];
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld char serial[sizeof (nvme->n_idctl->id_serial) + 1];
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bcopy(nvme->n_idctl->id_model, model, sizeof (nvme->n_idctl->id_model));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bcopy(nvme->n_idctl->id_serial, serial,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme->n_idctl->id_serial));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld model[sizeof (nvme->n_idctl->id_model)] = '\0';
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld serial[sizeof (nvme->n_idctl->id_serial)] = '\0';
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) snprintf(nvme->n_ns[nsid - 1].ns_devid,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme->n_ns[0].ns_devid), "%4X-%s-%s-%X",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_idctl->id_vid, model, serial, nsid);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_init(nvme_t *nvme)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_cc_t cc = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_aqa_t aqa = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_asq_t asq = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_acq_t acq = { 0 };
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_cap_t cap;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_vs_t vs;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_reg_csts_t csts;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int nqueues;
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld char model[sizeof (nvme->n_idctl->id_model) + 1];
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld char *vendor, *product;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Setup fixed interrupt for admin queue. */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_setup_interrupts(nvme, DDI_INTR_TYPE_FIXED, 1)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to setup fixed interrupt");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* Check controller version */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld vs.r = nvme_get32(nvme, NVME_REG_VS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_CONT, "?NVMe spec version %d.%d",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld vs.b.vs_mjr, vs.b.vs_mnr);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
2797e2b9652d58879341d37f99d68c4db73672fdHans Rosenfeld if (nvme_version_major < vs.b.vs_mjr ||
2797e2b9652d58879341d37f99d68c4db73672fdHans Rosenfeld (nvme_version_major == vs.b.vs_mjr &&
2797e2b9652d58879341d37f99d68c4db73672fdHans Rosenfeld nvme_version_minor < vs.b.vs_mnr)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!no support for version > %d.%d",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_version_major, nvme_version_minor);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_strict_version)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* retrieve controller configuration */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cap.r = nvme_get64(nvme, NVME_REG_CAP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if ((cap.b.cap_css & NVME_CAP_CSS_NVM) == 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!NVM command set not supported by hardware");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_nssr_supported = cap.b.cap_nssrs;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_doorbell_stride = 4 << cap.b.cap_dstrd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_timeout = cap.b.cap_to;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_arbitration_mechanisms = cap.b.cap_ams;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_cont_queues_reqd = cap.b.cap_cqr;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_max_queue_entries = cap.b.cap_mqes + 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The MPSMIN and MPSMAX fields in the CAP register use 0 to specify
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * the base page size of 4k (1<<12), so add 12 here to get the real
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * page size value.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_pageshift = MIN(MAX(cap.b.cap_mpsmin + 12, PAGESHIFT),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cap.b.cap_mpsmax + 12);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_pagesize = 1UL << (nvme->n_pageshift);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Set up Queue DMA to transfer at least 1 page-aligned page at a time.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_queue_dma_attr.dma_attr_align = nvme->n_pagesize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_queue_dma_attr.dma_attr_minxfer = nvme->n_pagesize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Set up PRP DMA to transfer 1 page-aligned page at a time.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Maxxfer may be increased after we identified the controller limits.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_prp_dma_attr.dma_attr_maxxfer = nvme->n_pagesize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_prp_dma_attr.dma_attr_minxfer = nvme->n_pagesize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_prp_dma_attr.dma_attr_align = nvme->n_pagesize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Reset controller if it's still in ready state.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_reset(nvme, B_FALSE) == B_FALSE) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!unable to reset controller");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Create the admin queue pair.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_alloc_qpair(nvme, nvme->n_admin_queue_len, &nvme->n_adminq, 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!unable to allocate admin qpair");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ioq = kmem_alloc(sizeof (nvme_qpair_t *), KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ioq[0] = nvme->n_adminq;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_progress |= NVME_ADMIN_QUEUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_prop_update_int(DDI_DEV_T_NONE, nvme->n_dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "admin-queue-len", nvme->n_admin_queue_len);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld aqa.b.aqa_asqs = aqa.b.aqa_acqs = nvme->n_admin_queue_len - 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld asq = nvme->n_adminq->nq_sqdma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld acq = nvme->n_adminq->nq_cqdma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT((asq & (nvme->n_pagesize - 1)) == 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT((acq & (nvme->n_pagesize - 1)) == 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_AQA, aqa.r);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put64(nvme, NVME_REG_ASQ, asq);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put64(nvme, NVME_REG_ACQ, acq);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.b.cc_ams = 0; /* use Round-Robin arbitration */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.b.cc_css = 0; /* use NVM command set */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.b.cc_mps = nvme->n_pageshift - 12;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.b.cc_shn = 0; /* no shutdown in progress */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cc.b.cc_en = 1; /* enable controller */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_put32(nvme, NVME_REG_CC, cc.r);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Wait for the controller to become ready.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_rdy == 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != nvme->n_timeout * 10; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld delay(drv_usectohz(50000));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld csts.r = nvme_get32(nvme, NVME_REG_CSTS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_cfs == 1) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!controller fatal status at init");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(nvme->n_dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_rdy == 1)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (csts.b.csts_rdy == 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!controller not ready");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_service_impact(nvme->n_dip, DDI_SERVICE_LOST);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dead = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Assume an abort command limit of 1. We'll destroy and re-init
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * that later when we know the true abort command limit.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_init(&nvme->n_abort_sema, 1, NULL, SEMA_DRIVER, NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Post an asynchronous event command to catch errors.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_async_event(nvme) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to post async event");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Identify Controller
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_idctl = nvme_identify(nvme, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_idctl == NULL) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to identify controller");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld /*
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld * Get Vendor & Product ID
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld */
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld bcopy(nvme->n_idctl->id_model, model, sizeof (nvme->n_idctl->id_model));
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld model[sizeof (nvme->n_idctl->id_model)] = '\0';
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld sata_split_model(model, &vendor, &product);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld if (vendor == NULL)
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld nvme->n_vendor = strdup("NVMe");
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld else
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld nvme->n_vendor = strdup(vendor);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld nvme->n_product = strdup(product);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Get controller limits.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_async_event_limit = MAX(NVME_MIN_ASYNC_EVENT_LIMIT,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld MIN(nvme->n_admin_queue_len / 10,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld MIN(nvme->n_idctl->id_aerl + 1, nvme->n_async_event_limit)));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_prop_update_int(DDI_DEV_T_NONE, nvme->n_dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "async-event-limit", nvme->n_async_event_limit);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_abort_command_limit = nvme->n_idctl->id_acl + 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld /*
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld * Reinitialize the semaphore with the true abort command limit
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld * supported by the hardware. It's not necessary to disable interrupts
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld * as only command aborts use the semaphore, and no commands are
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld * executed or aborted while we're here.
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_destroy(&nvme->n_abort_sema);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_init(&nvme->n_abort_sema, nvme->n_abort_command_limit - 1, NULL,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld SEMA_DRIVER, NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_progress |= NVME_CTRL_LIMITS;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_idctl->id_mdts == 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_max_data_transfer_size = nvme->n_pagesize * 65536;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_max_data_transfer_size =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld 1ull << (nvme->n_pageshift + nvme->n_idctl->id_mdts);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_error_log_len = nvme->n_idctl->id_elpe + 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Limit n_max_data_transfer_size to what we can handle in one PRP.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Chained PRPs are currently unsupported.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * This is a no-op on hardware which doesn't support a transfer size
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * big enough to require chained PRPs.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_max_data_transfer_size = MIN(nvme->n_max_data_transfer_size,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (nvme->n_pagesize / sizeof (uint64_t) * nvme->n_pagesize));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_prp_dma_attr.dma_attr_maxxfer = nvme->n_max_data_transfer_size;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Make sure the minimum/maximum queue entry sizes are not
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * larger/smaller than the default.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (((1 << nvme->n_idctl->id_sqes.qes_min) > sizeof (nvme_sqe_t)) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ((1 << nvme->n_idctl->id_sqes.qes_max) < sizeof (nvme_sqe_t)) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ((1 << nvme->n_idctl->id_cqes.qes_min) > sizeof (nvme_cqe_t)) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ((1 << nvme->n_idctl->id_cqes.qes_max) < sizeof (nvme_cqe_t)))
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Check for the presence of a Volatile Write Cache. If present,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * enable it by default.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_idctl->id_vwc.vwc_present == 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_volatile_write_cache_enabled = B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_bd_ops.o_sync_cache = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld } else {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * TODO: send SET FEATURES to enable VWC
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * (have no hardware to test this)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_volatile_write_cache_enabled = B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_bd_ops.o_sync_cache = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Grab a copy of all mandatory log pages.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * TODO: should go away once user space tool exists to print logs
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_error_log = (nvme_error_log_entry_t *)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_get_logpage(nvme, NVME_LOGPAGE_ERROR);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_health_log = (nvme_health_log_t *)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_get_logpage(nvme, NVME_LOGPAGE_HEALTH, -1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_fwslot_log = (nvme_fwslot_log_t *)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_get_logpage(nvme, NVME_LOGPAGE_FWSLOT);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Identify Namespaces
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_namespace_count = nvme->n_idctl->id_nn;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns = kmem_zalloc(sizeof (nvme_namespace_t) *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_namespace_count, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != nvme->n_namespace_count; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_identify_nsid_t *idns;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int last_rp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_nvme = nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_idns = idns = nvme_identify(nvme, i + 1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (idns == NULL) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to identify namespace %d", i + 1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_id = i + 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_block_count = idns->id_nsize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_block_size =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld 1 << idns->id_lbaf[idns->id_flbas.lba_format].lbaf_lbads;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_best_block_size = nvme->n_ns[i].ns_block_size;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_prepare_devid(nvme, nvme->n_ns[i].ns_id);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Find the LBA format with no metadata and the best relative
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * performance. A value of 3 means "degraded", 0 is best.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld last_rp = 3;
0d140ff944190d3ff2fdba65e9864f3c03248162Hans Rosenfeld for (int j = 0; j <= idns->id_nlbaf; j++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (idns->id_lbaf[j].lbaf_lbads == 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (idns->id_lbaf[j].lbaf_ms != 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld continue;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (idns->id_lbaf[j].lbaf_rp >= last_rp)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld continue;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld last_rp = idns->id_lbaf[j].lbaf_rp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_best_block_size =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld 1 << idns->id_lbaf[j].lbaf_lbads;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * We currently don't support namespaces that use either:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - thin provisioning
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - extended LBAs
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * - protection information
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (idns->id_nsfeat.f_thin ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld idns->id_flbas.lba_extlba ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld idns->id_dps.dp_pinfo) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!ignoring namespace %d, unsupported features: "
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "thin = %d, extlba = %d, pinfo = %d", i + 1,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld idns->id_nsfeat.f_thin, idns->id_flbas.lba_extlba,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld idns->id_dps.dp_pinfo);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_ignore = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Try to set up MSI/MSI-X interrupts.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if ((nvme->n_intr_types & (DDI_INTR_TYPE_MSI | DDI_INTR_TYPE_MSIX))
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_release_interrupts(nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nqueues = MIN(UINT16_MAX, ncpus);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if ((nvme_setup_interrupts(nvme, DDI_INTR_TYPE_MSIX,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nqueues) != DDI_SUCCESS) &&
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (nvme_setup_interrupts(nvme, DDI_INTR_TYPE_MSI,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nqueues) != DDI_SUCCESS)) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to setup MSI/MSI-X interrupts");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nqueues = nvme->n_intr_cnt;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Create I/O queue pairs.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ioq_count = nvme_set_nqueues(nvme, nqueues);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ioq_count == 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to set number of I/O queues to %d", nqueues);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Reallocate I/O queue array
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(nvme->n_ioq, sizeof (nvme_qpair_t *));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ioq = kmem_zalloc(sizeof (nvme_qpair_t *) *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (nvme->n_ioq_count + 1), KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ioq[0] = nvme->n_adminq;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * If we got less queues than we asked for we might as well give
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * some of the interrupt vectors back to the system.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ioq_count < nqueues) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_release_interrupts(nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_setup_interrupts(nvme, nvme->n_intr_type, nqueues)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to reduce number of interrupts");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Alloc & register I/O queue pairs
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_io_queue_len =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld MIN(nvme->n_io_queue_len, nvme->n_max_queue_entries);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_prop_update_int(DDI_DEV_T_NONE, nvme->n_dip, "io-queue-len",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_io_queue_len);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 1; i != nvme->n_ioq_count + 1; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_alloc_qpair(nvme, nvme->n_io_queue_len,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &nvme->n_ioq[i], i) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!unable to allocate I/O qpair %d", i);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_create_io_qpair(nvme, nvme->n_ioq[i], i)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!unable to create I/O qpair %d", i);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Post more asynchronous events commands to reduce event reporting
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * latency as suggested by the spec.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 1; i != nvme->n_async_event_limit; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_async_event(nvme) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to post async event %d", i);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) nvme_reset(nvme, B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic uint_t
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_intr(caddr_t arg1, caddr_t arg2)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*LINTED: E_PTR_BAD_CAST_ALIGN*/
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = (nvme_t *)arg1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int inum = (int)(uintptr_t)arg2;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int qnum;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (inum >= nvme->n_intr_cnt)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_INTR_UNCLAIMED);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The interrupt vector a queue uses is calculated as queue_idx %
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * intr_cnt in nvme_create_io_qpair(). Iterate through the queue array
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * in steps of n_intr_cnt to process all queues using this vector.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (qnum = inum;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qnum < nvme->n_ioq_count + 1 && nvme->n_ioq[qnum] != NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld qnum += nvme->n_intr_cnt) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld while ((cmd = nvme_retrieve_cmd(nvme, nvme->n_ioq[qnum]))) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld taskq_dispatch_ent((taskq_t *)cmd->nc_nvme->n_cmd_taskq,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback, cmd, TQ_NOSLEEP, &cmd->nc_tqent);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_INTR_CLAIMED);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeldnvme_release_interrupts(nvme_t *nvme)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i < nvme->n_intr_cnt; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_inth[i] == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_intr_cap & DDI_INTR_FLAG_BLOCK)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_intr_block_disable(&nvme->n_inth[i], 1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_intr_disable(nvme->n_inth[i]);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_intr_remove_handler(nvme->n_inth[i]);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_intr_free(nvme->n_inth[i]);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(nvme->n_inth, nvme->n_inth_sz);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_inth = NULL;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_inth_sz = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_progress &= ~NVME_INTERRUPTS;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_setup_interrupts(nvme_t *nvme, int intr_type, int nqpairs)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld int failed = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int nintrs, navail, count;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int ret;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_intr_types == 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = ddi_intr_get_supported_types(nvme->n_dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &nvme->n_intr_types);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!%s: ddi_intr_get_supported types failed",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ret);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if ((nvme->n_intr_types & intr_type) == 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = ddi_intr_get_nintrs(nvme->n_dip, intr_type, &nintrs);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!%s: ddi_intr_get_nintrs failed",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ret);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = ddi_intr_get_navail(nvme->n_dip, intr_type, &navail);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!%s: ddi_intr_get_navail failed",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ret);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* We want at most one interrupt per queue pair. */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (navail > nqpairs)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld navail = nqpairs;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_inth_sz = sizeof (ddi_intr_handle_t) * navail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_inth = kmem_zalloc(nvme->n_inth_sz, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = ddi_intr_alloc(nvme->n_dip, nvme->n_inth, intr_type, 0, navail,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &count, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!%s: ddi_intr_alloc failed",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_intr_cnt = count;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = ddi_intr_get_pri(nvme->n_inth[0], &nvme->n_intr_pri);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!%s: ddi_intr_get_pri failed",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i < count; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ret = ddi_intr_add_handler(nvme->n_inth[i], nvme_intr,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void *)nvme, (void *)(uintptr_t)i);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ret != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!%s: ddi_intr_add_handler failed", __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_intr_get_cap(nvme->n_inth[0], &nvme->n_intr_cap);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld for (i = 0; i < count; i++) {
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld if (nvme->n_inth[i] == NULL)
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld if (nvme->n_intr_cap & DDI_INTR_FLAG_BLOCK) {
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld if (ddi_intr_block_enable(&nvme->n_inth[i], 1) !=
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld DDI_SUCCESS)
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld failed++;
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld } else {
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld if (ddi_intr_enable(nvme->n_inth[i]) != DDI_SUCCESS)
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld failed++;
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld }
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld }
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld if (failed != 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN,
6afc9eb26c8915821607cde21b0a32addc165a72Hans Rosenfeld "!%s: enabling interrupts failed", __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_intr_type = intr_type;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_progress |= NVME_INTERRUPTS;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_release_interrupts(nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ret);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_fm_errcb(dev_info_t *dip, ddi_fm_error_t *fm_error, const void *arg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld _NOTE(ARGUNUSED(arg));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld pci_ereport_post(dip, fm_error, NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (fm_error->fme_status);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int instance;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int nregs;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld off_t regsize;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld char name[32];
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd != DDI_ATTACH)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld instance = ddi_get_instance(dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ddi_soft_state_zalloc(nvme_state, instance) != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme = ddi_get_soft_state(nvme_state, instance);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_set_driver_private(dip, nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_dip = dip;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_strict_version = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_PROP_DONTPASS, "strict-version", 1) == 1 ? B_TRUE : B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ignore_unknown_vendor_status = ddi_prop_get_int(DDI_DEV_T_ANY,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dip, DDI_PROP_DONTPASS, "ignore-unknown-vendor-status", 0) == 1 ?
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld B_TRUE : B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_admin_queue_len = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_PROP_DONTPASS, "admin-queue-len", NVME_DEFAULT_ADMIN_QUEUE_LEN);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_io_queue_len = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_PROP_DONTPASS, "io-queue-len", NVME_DEFAULT_IO_QUEUE_LEN);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_async_event_limit = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_PROP_DONTPASS, "async-event-limit",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld NVME_DEFAULT_ASYNC_EVENT_LIMIT);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_admin_queue_len < NVME_MIN_ADMIN_QUEUE_LEN)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_admin_queue_len = NVME_MIN_ADMIN_QUEUE_LEN;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld else if (nvme->n_admin_queue_len > NVME_MAX_ADMIN_QUEUE_LEN)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_admin_queue_len = NVME_MAX_ADMIN_QUEUE_LEN;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_io_queue_len < NVME_MIN_IO_QUEUE_LEN)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_io_queue_len = NVME_MIN_IO_QUEUE_LEN;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_async_event_limit < 1)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_async_event_limit = NVME_DEFAULT_ASYNC_EVENT_LIMIT;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_reg_acc_attr = nvme_reg_acc_attr;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_queue_dma_attr = nvme_queue_dma_attr;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_prp_dma_attr = nvme_prp_dma_attr;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_sgl_dma_attr = nvme_sgl_dma_attr;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Setup FMA support.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_fm_cap = ddi_getprop(DDI_DEV_T_ANY, dip,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_init(dip, &nvme->n_fm_cap, &nvme->n_fm_ibc);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_fm_cap) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_fm_cap & DDI_FM_ACCCHK_CAPABLE)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_reg_acc_attr.devacc_attr_access =
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_FLAGERR_ACC;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_fm_cap & DDI_FM_DMACHK_CAPABLE) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_prp_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_sgl_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (DDI_FM_EREPORT_CAP(nvme->n_fm_cap) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_FM_ERRCB_CAP(nvme->n_fm_cap))
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld pci_ereport_setup(dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (DDI_FM_ERRCB_CAP(nvme->n_fm_cap))
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_handler_register(dip, nvme_fm_errcb,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void *)nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_progress |= NVME_FMA_INIT;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * The spec defines several register sets. Only the controller
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * registers (set 1) are currently used.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ddi_dev_nregs(dip, &nregs) == DDI_FAILURE ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nregs < 2 ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dev_regsize(dip, 1, &regsize) == DDI_FAILURE)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ddi_regs_map_setup(dip, 1, &nvme->n_regs, 0, regsize,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &nvme->n_reg_acc_attr, &nvme->n_regh) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(dip, CE_WARN, "!failed to map regset 1");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_progress |= NVME_REGS_MAPPED;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Create taskq for command completion.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) snprintf(name, sizeof (name), "%s%d_cmd_taskq",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_driver_name(dip), ddi_get_instance(dip));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_cmd_taskq = ddi_taskq_create(dip, name, MIN(UINT16_MAX, ncpus),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld TASKQ_DEFAULTPRI, 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_cmd_taskq == NULL) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(dip, CE_WARN, "!failed to create cmd taskq");
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_init(nvme) != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Attach the blkdev driver for each namespace.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != nvme->n_namespace_count; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ns[i].ns_ignore)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld continue;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_bd_hdl = bd_alloc_handle(&nvme->n_ns[i],
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &nvme_bd_ops, &nvme->n_prp_dma_attr, KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ns[i].ns_bd_hdl == NULL) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to get blkdev handle for namespace %d", i);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (bd_attach_handle(dip, nvme->n_ns[i].ns_bd_hdl)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(dip, CE_WARN,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld "!failed to attach blkdev handle for namespace %d",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld i);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* attach successful anyway so that FMA can retire the device */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_dead)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) nvme_detach(dip, DDI_DETACH);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int instance, i;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd != DDI_DETACH)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld instance = ddi_get_instance(dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme = ddi_get_soft_state(nvme_state, instance);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ns) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 0; i != nvme->n_namespace_count; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ns[i].ns_bd_hdl) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) bd_detach_handle(
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_ns[i].ns_bd_hdl);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_free_handle(nvme->n_ns[i].ns_bd_hdl);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ns[i].ns_idns)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(nvme->n_ns[i].ns_idns,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sizeof (nvme_identify_nsid_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(nvme->n_ns, sizeof (nvme_namespace_t) *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme->n_namespace_count);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_progress & NVME_INTERRUPTS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_release_interrupts(nvme);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_cmd_taskq)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_taskq_wait(nvme->n_cmd_taskq);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ioq_count > 0) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (i = 1; i != nvme->n_ioq_count + 1; i++) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_ioq[i] != NULL) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* TODO: send destroy queue commands */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_qpair(nvme->n_ioq[i]);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(nvme->n_ioq, sizeof (nvme_qpair_t *) *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (nvme->n_ioq_count + 1));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_progress & NVME_REGS_MAPPED) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_shutdown(nvme, NVME_CC_SHN_NORMAL, B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) nvme_reset(nvme, B_FALSE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_cmd_taskq)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_taskq_destroy(nvme->n_cmd_taskq);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_progress & NVME_CTRL_LIMITS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld sema_destroy(&nvme->n_abort_sema);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_progress & NVME_ADMIN_QUEUE)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_qpair(nvme->n_adminq);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_idctl)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld kmem_free(nvme->n_idctl, sizeof (nvme_identify_ctrl_t));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_progress & NVME_REGS_MAPPED)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_regs_map_free(&nvme->n_regh);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_progress & NVME_FMA_INIT) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (DDI_FM_ERRCB_CAP(nvme->n_fm_cap))
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_handler_unregister(nvme->n_dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (DDI_FM_EREPORT_CAP(nvme->n_fm_cap) ||
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_FM_ERRCB_CAP(nvme->n_fm_cap))
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld pci_ereport_teardown(nvme->n_dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_fm_fini(nvme->n_dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld if (nvme->n_vendor != NULL)
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld strfree(nvme->n_vendor);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld if (nvme->n_product != NULL)
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld strfree(nvme->n_product);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_soft_state_free(nvme_state, instance);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_quiesce(dev_info_t *dip)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int instance;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld instance = ddi_get_instance(dip);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme = ddi_get_soft_state(nvme_state, instance);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_shutdown(nvme, NVME_CC_SHN_ABRUPT, B_TRUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) nvme_reset(nvme, B_TRUE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_fill_prp(nvme_cmd_t *cmd, bd_xfer_t *xfer)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = cmd->nc_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int nprp_page, nprp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld uint64_t *prp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (xfer->x_ndmac == 0)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[0] = xfer->x_dmac.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_nextcookie(xfer->x_dmah, &xfer->x_dmac);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (xfer->x_ndmac == 1) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[1] = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld } else if (xfer->x_ndmac == 2) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[1] = xfer->x_dmac.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld xfer->x_ndmac--;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nprp_page = nvme->n_pagesize / sizeof (uint64_t) - 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(nprp_page > 0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nprp = (xfer->x_ndmac + nprp_page - 1) / nprp_page;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * We currently don't support chained PRPs and set up our DMA
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * attributes to reflect that. If we still get an I/O request
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * that needs a chained PRP something is very wrong.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld VERIFY(nprp == 1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_zalloc_dma(nvme, nvme->n_pagesize * nprp, DDI_DMA_READ,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld &nvme->n_prp_dma_attr, &cmd->nc_dma) != DDI_SUCCESS) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld dev_err(nvme->n_dip, CE_WARN, "!%s: nvme_zalloc_dma failed",
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld __func__);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_FAILURE);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_dptr.d_prp[1] = cmd->nc_dma->nd_cookie.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_nextcookie(cmd->nc_dma->nd_dmah, &cmd->nc_dma->nd_cookie);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*LINTED: E_PTR_BAD_CAST_ALIGN*/
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld for (prp = (uint64_t *)cmd->nc_dma->nd_memp;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld xfer->x_ndmac > 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld prp++, xfer->x_ndmac--) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld *prp = xfer->x_dmac.dmac_laddress;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ddi_dma_nextcookie(xfer->x_dmah, &xfer->x_dmac);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld (void) ddi_dma_sync(cmd->nc_dma->nd_dmah, 0, cmd->nc_dma->nd_len,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld DDI_DMA_SYNC_FORDEV);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (DDI_SUCCESS);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic nvme_cmd_t *
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_create_nvm_cmd(nvme_namespace_t *ns, uint8_t opc, bd_xfer_t *xfer)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = ns->ns_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * Blkdev only sets BD_XFER_POLL when dumping, so don't sleep.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd = nvme_alloc_cmd(nvme, (xfer->x_flags & BD_XFER_POLL) ?
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld KM_NOSLEEP : KM_SLEEP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_opc = opc;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_callback = nvme_bd_xfer_done;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_xfer = xfer;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld switch (opc) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_OPC_NVM_WRITE:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_OPC_NVM_READ:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld VERIFY(xfer->x_nblks <= 0x10000);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_nsid = ns->ns_id;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw10 = xfer->x_blkno & 0xffffffffu;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw11 = (xfer->x_blkno >> 32);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_cdw12 = (uint16_t)(xfer->x_nblks - 1);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_fill_prp(cmd, xfer) != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld case NVME_OPC_NVM_FLUSH:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqe.sqe_nsid = ns->ns_id;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld break;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld default:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld goto fail;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldfail:
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (NULL);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_xfer_done(void *arg)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_xfer_t *xfer = cmd->nc_xfer;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld int error = 0;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld error = nvme_check_cmd_status(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_free_cmd(cmd);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_xfer_done(xfer, error);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic void
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_driveinfo(void *arg, bd_drive_t *drive)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_namespace_t *ns = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = ns->ns_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * blkdev maintains one queue size per instance (namespace),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * but all namespace share the I/O queues.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * TODO: need to figure out a sane default, or use per-NS I/O queues,
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * or change blkdev to handle EAGAIN
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drive->d_qsize = nvme->n_ioq_count * nvme->n_io_queue_len
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld / nvme->n_namespace_count;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * d_maxxfer is not set, which means the value is taken from the DMA
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * attributes specified to bd_alloc_handle.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drive->d_removable = B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drive->d_hotpluggable = B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drive->d_target = ns->ns_id;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld drive->d_lun = 0;
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld drive->d_model = nvme->n_idctl->id_model;
bef9e21af8ac4992b7dfef56530c7b0ed9184096Hans Rosenfeld drive->d_model_len = sizeof (nvme->n_idctl->id_model);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_vendor = nvme->n_vendor;
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_vendor_len = strlen(nvme->n_vendor);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_product = nvme->n_product;
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_product_len = strlen(nvme->n_product);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_serial = nvme->n_idctl->id_serial;
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_serial_len = sizeof (nvme->n_idctl->id_serial);
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_revision = nvme->n_idctl->id_fwrev;
510a68476ba6e33759b7603130d76db4cec783d1Hans Rosenfeld drive->d_revision_len = sizeof (nvme->n_idctl->id_fwrev);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_mediainfo(void *arg, bd_media_t *media)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_namespace_t *ns = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld media->m_nblks = ns->ns_block_count;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld media->m_blksize = ns->ns_block_size;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld media->m_readonly = B_FALSE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld media->m_solidstate = B_TRUE;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld media->m_pblksize = ns->ns_best_block_size;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_cmd(nvme_namespace_t *ns, bd_xfer_t *xfer, uint8_t opc)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_t *nvme = ns->ns_nvme;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_cmd_t *cmd;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme->n_dead)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /* No polling for now */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (xfer->x_flags & BD_XFER_POLL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd = nvme_create_nvm_cmd(ns, opc, xfer);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (cmd == NULL)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ENOMEM);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld cmd->nc_sqid = (CPU->cpu_id % nvme->n_ioq_count) + 1;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ASSERT(cmd->nc_sqid <= nvme->n_ioq_count);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (nvme_submit_cmd(nvme->n_ioq[cmd->nc_sqid], cmd)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld != DDI_SUCCESS)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EAGAIN);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_read(void *arg, bd_xfer_t *xfer)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_namespace_t *ns = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_bd_cmd(ns, xfer, NVME_OPC_NVM_READ));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_write(void *arg, bd_xfer_t *xfer)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_namespace_t *ns = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_bd_cmd(ns, xfer, NVME_OPC_NVM_WRITE));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_sync(void *arg, bd_xfer_t *xfer)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_namespace_t *ns = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ns->ns_nvme->n_dead)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (EIO);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld /*
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * If the volatile write cache isn't enabled the FLUSH command is a
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld * no-op, so we can take a shortcut here.
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld */
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld if (ns->ns_nvme->n_volatile_write_cache_enabled == B_FALSE) {
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld bd_xfer_done(xfer, ENOTSUP);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (0);
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld }
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (nvme_bd_cmd(ns, xfer, NVME_OPC_NVM_FLUSH));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldstatic int
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeldnvme_bd_devid(void *arg, dev_info_t *devinfo, ddi_devid_t *devid)
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld{
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld nvme_namespace_t *ns = arg;
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld return (ddi_devid_init(devinfo, DEVID_ENCAP, strlen(ns->ns_devid),
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld ns->ns_devid, devid));
3c9168fa8e9c30d55b3aa2fde74bd7da46df53f5Hans Rosenfeld}