xref: /freebsd/sys/dev/nvme/nvme_qpair.c (revision 96ad26eefb45c365d423b6dd9be05a1d70958dc1)
1bb0ec6b3SJim Harris /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4f42ca756SJim Harris  * Copyright (C) 2012-2014 Intel Corporation
5bb0ec6b3SJim Harris  * All rights reserved.
6bb0ec6b3SJim Harris  *
7bb0ec6b3SJim Harris  * Redistribution and use in source and binary forms, with or without
8bb0ec6b3SJim Harris  * modification, are permitted provided that the following conditions
9bb0ec6b3SJim Harris  * are met:
10bb0ec6b3SJim Harris  * 1. Redistributions of source code must retain the above copyright
11bb0ec6b3SJim Harris  *    notice, this list of conditions and the following disclaimer.
12bb0ec6b3SJim Harris  * 2. Redistributions in binary form must reproduce the above copyright
13bb0ec6b3SJim Harris  *    notice, this list of conditions and the following disclaimer in the
14bb0ec6b3SJim Harris  *    documentation and/or other materials provided with the distribution.
15bb0ec6b3SJim Harris  *
16bb0ec6b3SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bb0ec6b3SJim Harris  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bb0ec6b3SJim Harris  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bb0ec6b3SJim Harris  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bb0ec6b3SJim Harris  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bb0ec6b3SJim Harris  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bb0ec6b3SJim Harris  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bb0ec6b3SJim Harris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bb0ec6b3SJim Harris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bb0ec6b3SJim Harris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bb0ec6b3SJim Harris  * SUCH DAMAGE.
27bb0ec6b3SJim Harris  */
28bb0ec6b3SJim Harris 
29bb0ec6b3SJim Harris #include <sys/cdefs.h>
30bb0ec6b3SJim Harris __FBSDID("$FreeBSD$");
31bb0ec6b3SJim Harris 
32bb0ec6b3SJim Harris #include <sys/param.h>
33bb0ec6b3SJim Harris #include <sys/bus.h>
34d0aaeffdSWarner Losh #include <sys/conf.h>
351eab19cbSAlexander Motin #include <sys/domainset.h>
36d0aaeffdSWarner Losh #include <sys/proc.h>
37bb0ec6b3SJim Harris 
380f71ecf7SJim Harris #include <dev/pci/pcivar.h>
390f71ecf7SJim Harris 
40bb0ec6b3SJim Harris #include "nvme_private.h"
41bb0ec6b3SJim Harris 
422ffd6fceSWarner Losh typedef enum error_print { ERROR_PRINT_NONE, ERROR_PRINT_NO_RETRY, ERROR_PRINT_ALL } error_print_t;
432ffd6fceSWarner Losh #define DO_NOT_RETRY	1
442ffd6fceSWarner Losh 
45d6f54866SJim Harris static void	_nvme_qpair_submit_request(struct nvme_qpair *qpair,
46d6f54866SJim Harris 					   struct nvme_request *req);
47a965389bSScott Long static void	nvme_qpair_destroy(struct nvme_qpair *qpair);
48d6f54866SJim Harris 
49547d523eSJim Harris struct nvme_opcode_string {
50547d523eSJim Harris 
51547d523eSJim Harris 	uint16_t	opc;
52547d523eSJim Harris 	const char *	str;
53547d523eSJim Harris };
54547d523eSJim Harris 
55547d523eSJim Harris static struct nvme_opcode_string admin_opcode[] = {
56547d523eSJim Harris 	{ NVME_OPC_DELETE_IO_SQ, "DELETE IO SQ" },
57547d523eSJim Harris 	{ NVME_OPC_CREATE_IO_SQ, "CREATE IO SQ" },
58547d523eSJim Harris 	{ NVME_OPC_GET_LOG_PAGE, "GET LOG PAGE" },
59547d523eSJim Harris 	{ NVME_OPC_DELETE_IO_CQ, "DELETE IO CQ" },
60547d523eSJim Harris 	{ NVME_OPC_CREATE_IO_CQ, "CREATE IO CQ" },
61547d523eSJim Harris 	{ NVME_OPC_IDENTIFY, "IDENTIFY" },
62547d523eSJim Harris 	{ NVME_OPC_ABORT, "ABORT" },
63547d523eSJim Harris 	{ NVME_OPC_SET_FEATURES, "SET FEATURES" },
64547d523eSJim Harris 	{ NVME_OPC_GET_FEATURES, "GET FEATURES" },
65547d523eSJim Harris 	{ NVME_OPC_ASYNC_EVENT_REQUEST, "ASYNC EVENT REQUEST" },
66547d523eSJim Harris 	{ NVME_OPC_FIRMWARE_ACTIVATE, "FIRMWARE ACTIVATE" },
67547d523eSJim Harris 	{ NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD, "FIRMWARE IMAGE DOWNLOAD" },
686b1a96b1SAlexander Motin 	{ NVME_OPC_DEVICE_SELF_TEST, "DEVICE SELF-TEST" },
696b1a96b1SAlexander Motin 	{ NVME_OPC_NAMESPACE_ATTACHMENT, "NAMESPACE ATTACHMENT" },
706b1a96b1SAlexander Motin 	{ NVME_OPC_KEEP_ALIVE, "KEEP ALIVE" },
716b1a96b1SAlexander Motin 	{ NVME_OPC_DIRECTIVE_SEND, "DIRECTIVE SEND" },
726b1a96b1SAlexander Motin 	{ NVME_OPC_DIRECTIVE_RECEIVE, "DIRECTIVE RECEIVE" },
736b1a96b1SAlexander Motin 	{ NVME_OPC_VIRTUALIZATION_MANAGEMENT, "VIRTUALIZATION MANAGEMENT" },
746b1a96b1SAlexander Motin 	{ NVME_OPC_NVME_MI_SEND, "NVME-MI SEND" },
756b1a96b1SAlexander Motin 	{ NVME_OPC_NVME_MI_RECEIVE, "NVME-MI RECEIVE" },
766b1a96b1SAlexander Motin 	{ NVME_OPC_DOORBELL_BUFFER_CONFIG, "DOORBELL BUFFER CONFIG" },
77547d523eSJim Harris 	{ NVME_OPC_FORMAT_NVM, "FORMAT NVM" },
78547d523eSJim Harris 	{ NVME_OPC_SECURITY_SEND, "SECURITY SEND" },
79547d523eSJim Harris 	{ NVME_OPC_SECURITY_RECEIVE, "SECURITY RECEIVE" },
806b1a96b1SAlexander Motin 	{ NVME_OPC_SANITIZE, "SANITIZE" },
8190dfa8f0SAlexander Motin 	{ NVME_OPC_GET_LBA_STATUS, "GET LBA STATUS" },
82547d523eSJim Harris 	{ 0xFFFF, "ADMIN COMMAND" }
83547d523eSJim Harris };
84547d523eSJim Harris 
85547d523eSJim Harris static struct nvme_opcode_string io_opcode[] = {
86547d523eSJim Harris 	{ NVME_OPC_FLUSH, "FLUSH" },
87547d523eSJim Harris 	{ NVME_OPC_WRITE, "WRITE" },
88547d523eSJim Harris 	{ NVME_OPC_READ, "READ" },
89547d523eSJim Harris 	{ NVME_OPC_WRITE_UNCORRECTABLE, "WRITE UNCORRECTABLE" },
90547d523eSJim Harris 	{ NVME_OPC_COMPARE, "COMPARE" },
916b1a96b1SAlexander Motin 	{ NVME_OPC_WRITE_ZEROES, "WRITE ZEROES" },
92547d523eSJim Harris 	{ NVME_OPC_DATASET_MANAGEMENT, "DATASET MANAGEMENT" },
9390dfa8f0SAlexander Motin 	{ NVME_OPC_VERIFY, "VERIFY" },
946b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_REGISTER, "RESERVATION REGISTER" },
956b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_REPORT, "RESERVATION REPORT" },
966b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_ACQUIRE, "RESERVATION ACQUIRE" },
976b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_RELEASE, "RESERVATION RELEASE" },
98547d523eSJim Harris 	{ 0xFFFF, "IO COMMAND" }
99547d523eSJim Harris };
100547d523eSJim Harris 
101547d523eSJim Harris static const char *
102547d523eSJim Harris get_admin_opcode_string(uint16_t opc)
103547d523eSJim Harris {
104547d523eSJim Harris 	struct nvme_opcode_string *entry;
105547d523eSJim Harris 
106547d523eSJim Harris 	entry = admin_opcode;
107547d523eSJim Harris 
108547d523eSJim Harris 	while (entry->opc != 0xFFFF) {
109547d523eSJim Harris 		if (entry->opc == opc)
110547d523eSJim Harris 			return (entry->str);
111547d523eSJim Harris 		entry++;
112547d523eSJim Harris 	}
113547d523eSJim Harris 	return (entry->str);
114547d523eSJim Harris }
115547d523eSJim Harris 
116547d523eSJim Harris static const char *
117547d523eSJim Harris get_io_opcode_string(uint16_t opc)
118547d523eSJim Harris {
119547d523eSJim Harris 	struct nvme_opcode_string *entry;
120547d523eSJim Harris 
121547d523eSJim Harris 	entry = io_opcode;
122547d523eSJim Harris 
123547d523eSJim Harris 	while (entry->opc != 0xFFFF) {
124547d523eSJim Harris 		if (entry->opc == opc)
125547d523eSJim Harris 			return (entry->str);
126547d523eSJim Harris 		entry++;
127547d523eSJim Harris 	}
128547d523eSJim Harris 	return (entry->str);
129547d523eSJim Harris }
130547d523eSJim Harris 
131547d523eSJim Harris 
132547d523eSJim Harris static void
133547d523eSJim Harris nvme_admin_qpair_print_command(struct nvme_qpair *qpair,
134547d523eSJim Harris     struct nvme_command *cmd)
135547d523eSJim Harris {
136547d523eSJim Harris 
137547d523eSJim Harris 	nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%x "
138547d523eSJim Harris 	    "cdw10:%08x cdw11:%08x\n",
1399544e6dcSChuck Tuffli 	    get_admin_opcode_string(cmd->opc), cmd->opc, qpair->id, cmd->cid,
1400d787e9bSWojciech Macek 	    le32toh(cmd->nsid), le32toh(cmd->cdw10), le32toh(cmd->cdw11));
141547d523eSJim Harris }
142547d523eSJim Harris 
143547d523eSJim Harris static void
144547d523eSJim Harris nvme_io_qpair_print_command(struct nvme_qpair *qpair,
145547d523eSJim Harris     struct nvme_command *cmd)
146547d523eSJim Harris {
147547d523eSJim Harris 
1489544e6dcSChuck Tuffli 	switch (cmd->opc) {
149547d523eSJim Harris 	case NVME_OPC_WRITE:
150547d523eSJim Harris 	case NVME_OPC_READ:
151547d523eSJim Harris 	case NVME_OPC_WRITE_UNCORRECTABLE:
152547d523eSJim Harris 	case NVME_OPC_COMPARE:
1536b1a96b1SAlexander Motin 	case NVME_OPC_WRITE_ZEROES:
15490dfa8f0SAlexander Motin 	case NVME_OPC_VERIFY:
155547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d "
156bdd1fd40SJim Harris 		    "lba:%llu len:%d\n",
1579544e6dcSChuck Tuffli 		    get_io_opcode_string(cmd->opc), qpair->id, cmd->cid, le32toh(cmd->nsid),
1580d787e9bSWojciech Macek 		    ((unsigned long long)le32toh(cmd->cdw11) << 32) + le32toh(cmd->cdw10),
1590d787e9bSWojciech Macek 		    (le32toh(cmd->cdw12) & 0xFFFF) + 1);
160547d523eSJim Harris 		break;
161547d523eSJim Harris 	case NVME_OPC_FLUSH:
162547d523eSJim Harris 	case NVME_OPC_DATASET_MANAGEMENT:
1636b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_REGISTER:
1646b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_REPORT:
1656b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_ACQUIRE:
1666b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_RELEASE:
167547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n",
1689544e6dcSChuck Tuffli 		    get_io_opcode_string(cmd->opc), qpair->id, cmd->cid, le32toh(cmd->nsid));
169547d523eSJim Harris 		break;
170547d523eSJim Harris 	default:
171547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%d\n",
1729544e6dcSChuck Tuffli 		    get_io_opcode_string(cmd->opc), cmd->opc, qpair->id,
1730d787e9bSWojciech Macek 		    cmd->cid, le32toh(cmd->nsid));
174547d523eSJim Harris 		break;
175547d523eSJim Harris 	}
176547d523eSJim Harris }
177547d523eSJim Harris 
178547d523eSJim Harris static void
179547d523eSJim Harris nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd)
180547d523eSJim Harris {
181547d523eSJim Harris 	if (qpair->id == 0)
182547d523eSJim Harris 		nvme_admin_qpair_print_command(qpair, cmd);
183547d523eSJim Harris 	else
184547d523eSJim Harris 		nvme_io_qpair_print_command(qpair, cmd);
185c75bdc04SWarner Losh 	if (nvme_verbose_cmd_dump) {
186c75bdc04SWarner Losh 		nvme_printf(qpair->ctrlr,
187c75bdc04SWarner Losh 		    "nsid:%#x rsvd2:%#x rsvd3:%#x mptr:%#jx prp1:%#jx prp2:%#jx\n",
188c75bdc04SWarner Losh 		    cmd->nsid, cmd->rsvd2, cmd->rsvd3, (uintmax_t)cmd->mptr,
189c75bdc04SWarner Losh 		    (uintmax_t)cmd->prp1, (uintmax_t)cmd->prp2);
190c75bdc04SWarner Losh 		nvme_printf(qpair->ctrlr,
191c75bdc04SWarner Losh 		    "cdw10: %#x cdw11:%#x cdw12:%#x cdw13:%#x cdw14:%#x cdw15:%#x\n",
192c75bdc04SWarner Losh 		    cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14,
193c75bdc04SWarner Losh 		    cmd->cdw15);
194c75bdc04SWarner Losh 	}
195547d523eSJim Harris }
196547d523eSJim Harris 
197547d523eSJim Harris struct nvme_status_string {
198547d523eSJim Harris 
199547d523eSJim Harris 	uint16_t	sc;
200547d523eSJim Harris 	const char *	str;
201547d523eSJim Harris };
202547d523eSJim Harris 
203547d523eSJim Harris static struct nvme_status_string generic_status[] = {
204547d523eSJim Harris 	{ NVME_SC_SUCCESS, "SUCCESS" },
205547d523eSJim Harris 	{ NVME_SC_INVALID_OPCODE, "INVALID OPCODE" },
206547d523eSJim Harris 	{ NVME_SC_INVALID_FIELD, "INVALID_FIELD" },
207547d523eSJim Harris 	{ NVME_SC_COMMAND_ID_CONFLICT, "COMMAND ID CONFLICT" },
208547d523eSJim Harris 	{ NVME_SC_DATA_TRANSFER_ERROR, "DATA TRANSFER ERROR" },
209547d523eSJim Harris 	{ NVME_SC_ABORTED_POWER_LOSS, "ABORTED - POWER LOSS" },
210547d523eSJim Harris 	{ NVME_SC_INTERNAL_DEVICE_ERROR, "INTERNAL DEVICE ERROR" },
211547d523eSJim Harris 	{ NVME_SC_ABORTED_BY_REQUEST, "ABORTED - BY REQUEST" },
212547d523eSJim Harris 	{ NVME_SC_ABORTED_SQ_DELETION, "ABORTED - SQ DELETION" },
213547d523eSJim Harris 	{ NVME_SC_ABORTED_FAILED_FUSED, "ABORTED - FAILED FUSED" },
214547d523eSJim Harris 	{ NVME_SC_ABORTED_MISSING_FUSED, "ABORTED - MISSING FUSED" },
215547d523eSJim Harris 	{ NVME_SC_INVALID_NAMESPACE_OR_FORMAT, "INVALID NAMESPACE OR FORMAT" },
216547d523eSJim Harris 	{ NVME_SC_COMMAND_SEQUENCE_ERROR, "COMMAND SEQUENCE ERROR" },
2176b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_SGL_SEGMENT_DESCR, "INVALID SGL SEGMENT DESCRIPTOR" },
2186b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_NUMBER_OF_SGL_DESCR, "INVALID NUMBER OF SGL DESCRIPTORS" },
2196b1a96b1SAlexander Motin 	{ NVME_SC_DATA_SGL_LENGTH_INVALID, "DATA SGL LENGTH INVALID" },
2206b1a96b1SAlexander Motin 	{ NVME_SC_METADATA_SGL_LENGTH_INVALID, "METADATA SGL LENGTH INVALID" },
2216b1a96b1SAlexander Motin 	{ NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID, "SGL DESCRIPTOR TYPE INVALID" },
2226b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_USE_OF_CMB, "INVALID USE OF CONTROLLER MEMORY BUFFER" },
2236b1a96b1SAlexander Motin 	{ NVME_SC_PRP_OFFET_INVALID, "PRP OFFET INVALID" },
2246b1a96b1SAlexander Motin 	{ NVME_SC_ATOMIC_WRITE_UNIT_EXCEEDED, "ATOMIC WRITE UNIT EXCEEDED" },
2256b1a96b1SAlexander Motin 	{ NVME_SC_OPERATION_DENIED, "OPERATION DENIED" },
2266b1a96b1SAlexander Motin 	{ NVME_SC_SGL_OFFSET_INVALID, "SGL OFFSET INVALID" },
2276b1a96b1SAlexander Motin 	{ NVME_SC_HOST_ID_INCONSISTENT_FORMAT, "HOST IDENTIFIER INCONSISTENT FORMAT" },
2286b1a96b1SAlexander Motin 	{ NVME_SC_KEEP_ALIVE_TIMEOUT_EXPIRED, "KEEP ALIVE TIMEOUT EXPIRED" },
2296b1a96b1SAlexander Motin 	{ NVME_SC_KEEP_ALIVE_TIMEOUT_INVALID, "KEEP ALIVE TIMEOUT INVALID" },
2306b1a96b1SAlexander Motin 	{ NVME_SC_ABORTED_DUE_TO_PREEMPT, "COMMAND ABORTED DUE TO PREEMPT AND ABORT" },
2316b1a96b1SAlexander Motin 	{ NVME_SC_SANITIZE_FAILED, "SANITIZE FAILED" },
2326b1a96b1SAlexander Motin 	{ NVME_SC_SANITIZE_IN_PROGRESS, "SANITIZE IN PROGRESS" },
2336b1a96b1SAlexander Motin 	{ NVME_SC_SGL_DATA_BLOCK_GRAN_INVALID, "SGL_DATA_BLOCK_GRANULARITY_INVALID" },
2346b1a96b1SAlexander Motin 	{ NVME_SC_NOT_SUPPORTED_IN_CMB, "COMMAND NOT SUPPORTED FOR QUEUE IN CMB" },
23590dfa8f0SAlexander Motin 	{ NVME_SC_NAMESPACE_IS_WRITE_PROTECTED, "NAMESPACE IS WRITE PROTECTED" },
23690dfa8f0SAlexander Motin 	{ NVME_SC_COMMAND_INTERRUPTED, "COMMAND INTERRUPTED" },
23790dfa8f0SAlexander Motin 	{ NVME_SC_TRANSIENT_TRANSPORT_ERROR, "TRANSIENT TRANSPORT ERROR" },
2386b1a96b1SAlexander Motin 
239547d523eSJim Harris 	{ NVME_SC_LBA_OUT_OF_RANGE, "LBA OUT OF RANGE" },
240547d523eSJim Harris 	{ NVME_SC_CAPACITY_EXCEEDED, "CAPACITY EXCEEDED" },
241547d523eSJim Harris 	{ NVME_SC_NAMESPACE_NOT_READY, "NAMESPACE NOT READY" },
2426b1a96b1SAlexander Motin 	{ NVME_SC_RESERVATION_CONFLICT, "RESERVATION CONFLICT" },
2436b1a96b1SAlexander Motin 	{ NVME_SC_FORMAT_IN_PROGRESS, "FORMAT IN PROGRESS" },
244547d523eSJim Harris 	{ 0xFFFF, "GENERIC" }
245547d523eSJim Harris };
246547d523eSJim Harris 
247547d523eSJim Harris static struct nvme_status_string command_specific_status[] = {
248547d523eSJim Harris 	{ NVME_SC_COMPLETION_QUEUE_INVALID, "INVALID COMPLETION QUEUE" },
249547d523eSJim Harris 	{ NVME_SC_INVALID_QUEUE_IDENTIFIER, "INVALID QUEUE IDENTIFIER" },
250547d523eSJim Harris 	{ NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED, "MAX QUEUE SIZE EXCEEDED" },
251547d523eSJim Harris 	{ NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED, "ABORT CMD LIMIT EXCEEDED" },
252547d523eSJim Harris 	{ NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED, "ASYNC LIMIT EXCEEDED" },
253547d523eSJim Harris 	{ NVME_SC_INVALID_FIRMWARE_SLOT, "INVALID FIRMWARE SLOT" },
254547d523eSJim Harris 	{ NVME_SC_INVALID_FIRMWARE_IMAGE, "INVALID FIRMWARE IMAGE" },
255547d523eSJim Harris 	{ NVME_SC_INVALID_INTERRUPT_VECTOR, "INVALID INTERRUPT VECTOR" },
256547d523eSJim Harris 	{ NVME_SC_INVALID_LOG_PAGE, "INVALID LOG PAGE" },
257547d523eSJim Harris 	{ NVME_SC_INVALID_FORMAT, "INVALID FORMAT" },
258547d523eSJim Harris 	{ NVME_SC_FIRMWARE_REQUIRES_RESET, "FIRMWARE REQUIRES RESET" },
2596b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_QUEUE_DELETION, "INVALID QUEUE DELETION" },
2606b1a96b1SAlexander Motin 	{ NVME_SC_FEATURE_NOT_SAVEABLE, "FEATURE IDENTIFIER NOT SAVEABLE" },
2616b1a96b1SAlexander Motin 	{ NVME_SC_FEATURE_NOT_CHANGEABLE, "FEATURE NOT CHANGEABLE" },
2626b1a96b1SAlexander Motin 	{ NVME_SC_FEATURE_NOT_NS_SPECIFIC, "FEATURE NOT NAMESPACE SPECIFIC" },
2636b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_REQUIRES_NVMS_RESET, "FIRMWARE ACTIVATION REQUIRES NVM SUBSYSTEM RESET" },
2646b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_REQUIRES_RESET, "FIRMWARE ACTIVATION REQUIRES RESET" },
2656b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_REQUIRES_TIME, "FIRMWARE ACTIVATION REQUIRES MAXIMUM TIME VIOLATION" },
2666b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_PROHIBITED, "FIRMWARE ACTIVATION PROHIBITED" },
2676b1a96b1SAlexander Motin 	{ NVME_SC_OVERLAPPING_RANGE, "OVERLAPPING RANGE" },
2686b1a96b1SAlexander Motin 	{ NVME_SC_NS_INSUFFICIENT_CAPACITY, "NAMESPACE INSUFFICIENT CAPACITY" },
2696b1a96b1SAlexander Motin 	{ NVME_SC_NS_ID_UNAVAILABLE, "NAMESPACE IDENTIFIER UNAVAILABLE" },
2706b1a96b1SAlexander Motin 	{ NVME_SC_NS_ALREADY_ATTACHED, "NAMESPACE ALREADY ATTACHED" },
2716b1a96b1SAlexander Motin 	{ NVME_SC_NS_IS_PRIVATE, "NAMESPACE IS PRIVATE" },
2726b1a96b1SAlexander Motin 	{ NVME_SC_NS_NOT_ATTACHED, "NS NOT ATTACHED" },
2736b1a96b1SAlexander Motin 	{ NVME_SC_THIN_PROV_NOT_SUPPORTED, "THIN PROVISIONING NOT SUPPORTED" },
2746b1a96b1SAlexander Motin 	{ NVME_SC_CTRLR_LIST_INVALID, "CONTROLLER LIST INVALID" },
2756b1a96b1SAlexander Motin 	{ NVME_SC_SELT_TEST_IN_PROGRESS, "DEVICE SELT-TEST IN PROGRESS" },
2766b1a96b1SAlexander Motin 	{ NVME_SC_BOOT_PART_WRITE_PROHIB, "BOOT PARTITION WRITE PROHIBITED" },
2776b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_CTRLR_ID, "INVALID CONTROLLER IDENTIFIER" },
2786b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_SEC_CTRLR_STATE, "INVALID SECONDARY CONTROLLER STATE" },
2796b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_NUM_OF_CTRLR_RESRC, "INVALID NUMBER OF CONTROLLER RESOURCES" },
2806b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_RESOURCE_ID, "INVALID RESOURCE IDENTIFIER" },
28190dfa8f0SAlexander Motin 	{ NVME_SC_SANITIZE_PROHIBITED_WPMRE, "SANITIZE PROHIBITED WRITE PERSISTENT MEMORY REGION ENABLED" },
28290dfa8f0SAlexander Motin 	{ NVME_SC_ANA_GROUP_ID_INVALID, "ANA GROUP IDENTIFIED INVALID" },
28390dfa8f0SAlexander Motin 	{ NVME_SC_ANA_ATTACH_FAILED, "ANA ATTACH FAILED" },
2846b1a96b1SAlexander Motin 
285547d523eSJim Harris 	{ NVME_SC_CONFLICTING_ATTRIBUTES, "CONFLICTING ATTRIBUTES" },
286547d523eSJim Harris 	{ NVME_SC_INVALID_PROTECTION_INFO, "INVALID PROTECTION INFO" },
287547d523eSJim Harris 	{ NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE, "WRITE TO RO PAGE" },
288547d523eSJim Harris 	{ 0xFFFF, "COMMAND SPECIFIC" }
289547d523eSJim Harris };
290547d523eSJim Harris 
291547d523eSJim Harris static struct nvme_status_string media_error_status[] = {
292547d523eSJim Harris 	{ NVME_SC_WRITE_FAULTS, "WRITE FAULTS" },
293547d523eSJim Harris 	{ NVME_SC_UNRECOVERED_READ_ERROR, "UNRECOVERED READ ERROR" },
294547d523eSJim Harris 	{ NVME_SC_GUARD_CHECK_ERROR, "GUARD CHECK ERROR" },
295547d523eSJim Harris 	{ NVME_SC_APPLICATION_TAG_CHECK_ERROR, "APPLICATION TAG CHECK ERROR" },
296547d523eSJim Harris 	{ NVME_SC_REFERENCE_TAG_CHECK_ERROR, "REFERENCE TAG CHECK ERROR" },
297547d523eSJim Harris 	{ NVME_SC_COMPARE_FAILURE, "COMPARE FAILURE" },
298547d523eSJim Harris 	{ NVME_SC_ACCESS_DENIED, "ACCESS DENIED" },
2996b1a96b1SAlexander Motin 	{ NVME_SC_DEALLOCATED_OR_UNWRITTEN, "DEALLOCATED OR UNWRITTEN LOGICAL BLOCK" },
300547d523eSJim Harris 	{ 0xFFFF, "MEDIA ERROR" }
301547d523eSJim Harris };
302547d523eSJim Harris 
303a6d222ebSAlexander Motin static struct nvme_status_string path_related_status[] = {
304a6d222ebSAlexander Motin 	{ NVME_SC_INTERNAL_PATH_ERROR, "INTERNAL PATH ERROR" },
305a6d222ebSAlexander Motin 	{ NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS, "ASYMMETRIC ACCESS PERSISTENT LOSS" },
306a6d222ebSAlexander Motin 	{ NVME_SC_ASYMMETRIC_ACCESS_INACCESSIBLE, "ASYMMETRIC ACCESS INACCESSIBLE" },
307a6d222ebSAlexander Motin 	{ NVME_SC_ASYMMETRIC_ACCESS_TRANSITION, "ASYMMETRIC ACCESS TRANSITION" },
308a6d222ebSAlexander Motin 	{ NVME_SC_CONTROLLER_PATHING_ERROR, "CONTROLLER PATHING ERROR" },
309a6d222ebSAlexander Motin 	{ NVME_SC_HOST_PATHING_ERROR, "HOST PATHING ERROR" },
310a6d222ebSAlexander Motin 	{ NVME_SC_COMMAND_ABOTHED_BY_HOST, "COMMAND ABOTHED BY HOST" },
311a6d222ebSAlexander Motin 	{ 0xFFFF, "PATH RELATED" },
312a6d222ebSAlexander Motin };
313a6d222ebSAlexander Motin 
314547d523eSJim Harris static const char *
315547d523eSJim Harris get_status_string(uint16_t sct, uint16_t sc)
316547d523eSJim Harris {
317547d523eSJim Harris 	struct nvme_status_string *entry;
318547d523eSJim Harris 
319547d523eSJim Harris 	switch (sct) {
320547d523eSJim Harris 	case NVME_SCT_GENERIC:
321547d523eSJim Harris 		entry = generic_status;
322547d523eSJim Harris 		break;
323547d523eSJim Harris 	case NVME_SCT_COMMAND_SPECIFIC:
324547d523eSJim Harris 		entry = command_specific_status;
325547d523eSJim Harris 		break;
326547d523eSJim Harris 	case NVME_SCT_MEDIA_ERROR:
327547d523eSJim Harris 		entry = media_error_status;
328547d523eSJim Harris 		break;
329a6d222ebSAlexander Motin 	case NVME_SCT_PATH_RELATED:
330a6d222ebSAlexander Motin 		entry = path_related_status;
331a6d222ebSAlexander Motin 		break;
332547d523eSJim Harris 	case NVME_SCT_VENDOR_SPECIFIC:
333547d523eSJim Harris 		return ("VENDOR SPECIFIC");
334547d523eSJim Harris 	default:
335547d523eSJim Harris 		return ("RESERVED");
336547d523eSJim Harris 	}
337547d523eSJim Harris 
338547d523eSJim Harris 	while (entry->sc != 0xFFFF) {
339547d523eSJim Harris 		if (entry->sc == sc)
340547d523eSJim Harris 			return (entry->str);
341547d523eSJim Harris 		entry++;
342547d523eSJim Harris 	}
343547d523eSJim Harris 	return (entry->str);
344547d523eSJim Harris }
345547d523eSJim Harris 
346547d523eSJim Harris static void
347547d523eSJim Harris nvme_qpair_print_completion(struct nvme_qpair *qpair,
348547d523eSJim Harris     struct nvme_completion *cpl)
349547d523eSJim Harris {
3500d787e9bSWojciech Macek 	uint16_t sct, sc;
3510d787e9bSWojciech Macek 
3520d787e9bSWojciech Macek 	sct = NVME_STATUS_GET_SCT(cpl->status);
3530d787e9bSWojciech Macek 	sc = NVME_STATUS_GET_SC(cpl->status);
3540d787e9bSWojciech Macek 
355547d523eSJim Harris 	nvme_printf(qpair->ctrlr, "%s (%02x/%02x) sqid:%d cid:%d cdw0:%x\n",
3560d787e9bSWojciech Macek 	    get_status_string(sct, sc), sct, sc, cpl->sqid, cpl->cid,
3570d787e9bSWojciech Macek 	    cpl->cdw0);
358547d523eSJim Harris }
359547d523eSJim Harris 
3607588c6ccSWarner Losh static bool
3616cb06070SJim Harris nvme_completion_is_retry(const struct nvme_completion *cpl)
362bb0ec6b3SJim Harris {
3630d787e9bSWojciech Macek 	uint8_t sct, sc, dnr;
3640d787e9bSWojciech Macek 
3650d787e9bSWojciech Macek 	sct = NVME_STATUS_GET_SCT(cpl->status);
3660d787e9bSWojciech Macek 	sc = NVME_STATUS_GET_SC(cpl->status);
3672ffd6fceSWarner Losh 	dnr = NVME_STATUS_GET_DNR(cpl->status);	/* Do Not Retry Bit */
3680d787e9bSWojciech Macek 
369bb0ec6b3SJim Harris 	/*
370bb0ec6b3SJim Harris 	 * TODO: spec is not clear how commands that are aborted due
371bb0ec6b3SJim Harris 	 *  to TLER will be marked.  So for now, it seems
372bb0ec6b3SJim Harris 	 *  NAMESPACE_NOT_READY is the only case where we should
37395108cadSWarner Losh 	 *  look at the DNR bit. Requests failed with ABORTED_BY_REQUEST
37495108cadSWarner Losh 	 *  set the DNR bit correctly since the driver controls that.
375bb0ec6b3SJim Harris 	 */
3760d787e9bSWojciech Macek 	switch (sct) {
377bb0ec6b3SJim Harris 	case NVME_SCT_GENERIC:
3780d787e9bSWojciech Macek 		switch (sc) {
379448195e7SJim Harris 		case NVME_SC_ABORTED_BY_REQUEST:
380bb0ec6b3SJim Harris 		case NVME_SC_NAMESPACE_NOT_READY:
3810d787e9bSWojciech Macek 			if (dnr)
382bb0ec6b3SJim Harris 				return (0);
383bb0ec6b3SJim Harris 			else
384bb0ec6b3SJim Harris 				return (1);
385bb0ec6b3SJim Harris 		case NVME_SC_INVALID_OPCODE:
386bb0ec6b3SJim Harris 		case NVME_SC_INVALID_FIELD:
387bb0ec6b3SJim Harris 		case NVME_SC_COMMAND_ID_CONFLICT:
388bb0ec6b3SJim Harris 		case NVME_SC_DATA_TRANSFER_ERROR:
389bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_POWER_LOSS:
390bb0ec6b3SJim Harris 		case NVME_SC_INTERNAL_DEVICE_ERROR:
391bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_SQ_DELETION:
392bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_FAILED_FUSED:
393bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_MISSING_FUSED:
394bb0ec6b3SJim Harris 		case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
395bb0ec6b3SJim Harris 		case NVME_SC_COMMAND_SEQUENCE_ERROR:
396bb0ec6b3SJim Harris 		case NVME_SC_LBA_OUT_OF_RANGE:
397bb0ec6b3SJim Harris 		case NVME_SC_CAPACITY_EXCEEDED:
398bb0ec6b3SJim Harris 		default:
399bb0ec6b3SJim Harris 			return (0);
400bb0ec6b3SJim Harris 		}
401bb0ec6b3SJim Harris 	case NVME_SCT_COMMAND_SPECIFIC:
402bb0ec6b3SJim Harris 	case NVME_SCT_MEDIA_ERROR:
403a6d222ebSAlexander Motin 		return (0);
404a6d222ebSAlexander Motin 	case NVME_SCT_PATH_RELATED:
405a6d222ebSAlexander Motin 		switch (sc) {
406a6d222ebSAlexander Motin 		case NVME_SC_INTERNAL_PATH_ERROR:
407a6d222ebSAlexander Motin 			if (dnr)
408a6d222ebSAlexander Motin 				return (0);
409a6d222ebSAlexander Motin 			else
410a6d222ebSAlexander Motin 				return (1);
411a6d222ebSAlexander Motin 		default:
412a6d222ebSAlexander Motin 			return (0);
413a6d222ebSAlexander Motin 		}
414bb0ec6b3SJim Harris 	case NVME_SCT_VENDOR_SPECIFIC:
415bb0ec6b3SJim Harris 	default:
416bb0ec6b3SJim Harris 		return (0);
417bb0ec6b3SJim Harris 	}
418bb0ec6b3SJim Harris }
419bb0ec6b3SJim Harris 
42021b6da58SJim Harris static void
42143393e8bSWarner Losh nvme_qpair_complete_tracker(struct nvme_tracker *tr,
4222ffd6fceSWarner Losh     struct nvme_completion *cpl, error_print_t print_on_error)
423bb0ec6b3SJim Harris {
42443393e8bSWarner Losh 	struct nvme_qpair * qpair = tr->qpair;
425ad697276SJim Harris 	struct nvme_request	*req;
4267588c6ccSWarner Losh 	bool			retry, error, retriable;
427bb0ec6b3SJim Harris 
428ad697276SJim Harris 	req = tr->req;
4296cb06070SJim Harris 	error = nvme_completion_is_error(cpl);
4305e83c2ffSWarner Losh 	retriable = nvme_completion_is_retry(cpl);
4315e83c2ffSWarner Losh 	retry = error && retriable && req->retries < nvme_retry_count;
432c37fc318SWarner Losh 	if (retry)
433c37fc318SWarner Losh 		qpair->num_retries++;
4345e83c2ffSWarner Losh 	if (error && req->retries >= nvme_retry_count && retriable)
4355e83c2ffSWarner Losh 		qpair->num_failures++;
436ad697276SJim Harris 
4372ffd6fceSWarner Losh 	if (error && (print_on_error == ERROR_PRINT_ALL ||
4382ffd6fceSWarner Losh 		(!retry && print_on_error == ERROR_PRINT_NO_RETRY))) {
439547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
440547d523eSJim Harris 		nvme_qpair_print_completion(qpair, cpl);
441bb0ec6b3SJim Harris 	}
442bb0ec6b3SJim Harris 
443bb0ec6b3SJim Harris 	qpair->act_tr[cpl->cid] = NULL;
444bb0ec6b3SJim Harris 
4456cb06070SJim Harris 	KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n"));
446bb0ec6b3SJim Harris 
4470a4b14e8SMichal Meloun 	if (!retry) {
4480a4b14e8SMichal Meloun 		if (req->type != NVME_REQUEST_NULL) {
4490a4b14e8SMichal Meloun 			bus_dmamap_sync(qpair->dma_tag_payload,
4500a4b14e8SMichal Meloun 			    tr->payload_dma_map,
4510a4b14e8SMichal Meloun 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4520a4b14e8SMichal Meloun 		}
4530a4b14e8SMichal Meloun 		if (req->cb_fn)
454ad697276SJim Harris 			req->cb_fn(req->cb_arg, cpl);
4550a4b14e8SMichal Meloun 	}
456bb0ec6b3SJim Harris 
457bb0ec6b3SJim Harris 	mtx_lock(&qpair->lock);
458bb0ec6b3SJim Harris 	callout_stop(&tr->timer);
459bb0ec6b3SJim Harris 
460cb5b7c13SJim Harris 	if (retry) {
461cb5b7c13SJim Harris 		req->retries++;
462b846efd7SJim Harris 		nvme_qpair_submit_tracker(qpair, tr);
463cb5b7c13SJim Harris 	} else {
4642e0090afSJustin Hibbits 		if (req->type != NVME_REQUEST_NULL) {
465a6e30963SJim Harris 			bus_dmamap_unload(qpair->dma_tag_payload,
466f2b19f67SJim Harris 			    tr->payload_dma_map);
4672e0090afSJustin Hibbits 		}
468bb0ec6b3SJim Harris 
469ad697276SJim Harris 		nvme_free_request(req);
4700a0b08ccSJim Harris 		tr->req = NULL;
47121b6da58SJim Harris 
47265c2474eSJim Harris 		TAILQ_REMOVE(&qpair->outstanding_tr, tr, tailq);
47365c2474eSJim Harris 		TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
4740f71ecf7SJim Harris 
475f37c22a3SJim Harris 		/*
476f37c22a3SJim Harris 		 * If the controller is in the middle of resetting, don't
477f37c22a3SJim Harris 		 *  try to submit queued requests here - let the reset logic
478f37c22a3SJim Harris 		 *  handle that instead.
479f37c22a3SJim Harris 		 */
480f37c22a3SJim Harris 		if (!STAILQ_EMPTY(&qpair->queued_req) &&
481f37c22a3SJim Harris 		    !qpair->ctrlr->is_resetting) {
4820f71ecf7SJim Harris 			req = STAILQ_FIRST(&qpair->queued_req);
4830f71ecf7SJim Harris 			STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
484d6f54866SJim Harris 			_nvme_qpair_submit_request(qpair, req);
4850f71ecf7SJim Harris 		}
486c2e83b40SJim Harris 	}
487bb0ec6b3SJim Harris 
488bb0ec6b3SJim Harris 	mtx_unlock(&qpair->lock);
4896cb06070SJim Harris }
4906cb06070SJim Harris 
491b846efd7SJim Harris static void
49243393e8bSWarner Losh nvme_qpair_manual_complete_tracker(
493232e2edbSJim Harris     struct nvme_tracker *tr, uint32_t sct, uint32_t sc, uint32_t dnr,
4942ffd6fceSWarner Losh     error_print_t print_on_error)
495b846efd7SJim Harris {
496b846efd7SJim Harris 	struct nvme_completion	cpl;
497b846efd7SJim Harris 
498b846efd7SJim Harris 	memset(&cpl, 0, sizeof(cpl));
49943393e8bSWarner Losh 
50043393e8bSWarner Losh 	struct nvme_qpair * qpair = tr->qpair;
50143393e8bSWarner Losh 
502b846efd7SJim Harris 	cpl.sqid = qpair->id;
503b846efd7SJim Harris 	cpl.cid = tr->cid;
5040d787e9bSWojciech Macek 	cpl.status |= (sct & NVME_STATUS_SCT_MASK) << NVME_STATUS_SCT_SHIFT;
5050d787e9bSWojciech Macek 	cpl.status |= (sc & NVME_STATUS_SC_MASK) << NVME_STATUS_SC_SHIFT;
5060d787e9bSWojciech Macek 	cpl.status |= (dnr & NVME_STATUS_DNR_MASK) << NVME_STATUS_DNR_SHIFT;
50743393e8bSWarner Losh 	nvme_qpair_complete_tracker(tr, &cpl, print_on_error);
508b846efd7SJim Harris }
509b846efd7SJim Harris 
5106cb06070SJim Harris void
511232e2edbSJim Harris nvme_qpair_manual_complete_request(struct nvme_qpair *qpair,
5122ffd6fceSWarner Losh     struct nvme_request *req, uint32_t sct, uint32_t sc)
513232e2edbSJim Harris {
514232e2edbSJim Harris 	struct nvme_completion	cpl;
5157588c6ccSWarner Losh 	bool			error;
516232e2edbSJim Harris 
517232e2edbSJim Harris 	memset(&cpl, 0, sizeof(cpl));
518232e2edbSJim Harris 	cpl.sqid = qpair->id;
5190d787e9bSWojciech Macek 	cpl.status |= (sct & NVME_STATUS_SCT_MASK) << NVME_STATUS_SCT_SHIFT;
5200d787e9bSWojciech Macek 	cpl.status |= (sc & NVME_STATUS_SC_MASK) << NVME_STATUS_SC_SHIFT;
521232e2edbSJim Harris 
522232e2edbSJim Harris 	error = nvme_completion_is_error(&cpl);
523232e2edbSJim Harris 
5242ffd6fceSWarner Losh 	if (error) {
525547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
526547d523eSJim Harris 		nvme_qpair_print_completion(qpair, &cpl);
527232e2edbSJim Harris 	}
528232e2edbSJim Harris 
529232e2edbSJim Harris 	if (req->cb_fn)
530232e2edbSJim Harris 		req->cb_fn(req->cb_arg, &cpl);
531232e2edbSJim Harris 
532232e2edbSJim Harris 	nvme_free_request(req);
533232e2edbSJim Harris }
534232e2edbSJim Harris 
535d85d9648SWarner Losh bool
5366cb06070SJim Harris nvme_qpair_process_completions(struct nvme_qpair *qpair)
5376cb06070SJim Harris {
5386cb06070SJim Harris 	struct nvme_tracker	*tr;
5390d787e9bSWojciech Macek 	struct nvme_completion	cpl;
540d85d9648SWarner Losh 	int done = 0;
541d0aaeffdSWarner Losh 	bool in_panic = dumping || SCHEDULER_STOPPED();
5426cb06070SJim Harris 
5436cb06070SJim Harris 	qpair->num_intr_handler_calls++;
5446cb06070SJim Harris 
545b846efd7SJim Harris 	/*
546d0aaeffdSWarner Losh 	 * qpair is not enabled, likely because a controller reset is is in
547d0aaeffdSWarner Losh 	 * progress.  Ignore the interrupt - any I/O that was associated with
548d0aaeffdSWarner Losh 	 * this interrupt will get retried when the reset is complete.
549b846efd7SJim Harris 	 */
550d0aaeffdSWarner Losh 	if (!qpair->is_enabled)
551d85d9648SWarner Losh 		return (false);
552b846efd7SJim Harris 
553d0aaeffdSWarner Losh 	/*
554d0aaeffdSWarner Losh 	 * A panic can stop the CPU this routine is running on at any point.  If
555d0aaeffdSWarner Losh 	 * we're called during a panic, complete the sq_head wrap protocol for
556d0aaeffdSWarner Losh 	 * the case where we are interrupted just after the increment at 1
557d0aaeffdSWarner Losh 	 * below, but before we can reset cq_head to zero at 2. Also cope with
558d0aaeffdSWarner Losh 	 * the case where we do the zero at 2, but may or may not have done the
559d0aaeffdSWarner Losh 	 * phase adjustment at step 3. The panic machinery flushes all pending
560d0aaeffdSWarner Losh 	 * memory writes, so we can make these strong ordering assumptions
561d0aaeffdSWarner Losh 	 * that would otherwise be unwise if we were racing in real time.
562d0aaeffdSWarner Losh 	 */
563d0aaeffdSWarner Losh 	if (__predict_false(in_panic)) {
564d0aaeffdSWarner Losh 		if (qpair->cq_head == qpair->num_entries) {
565d0aaeffdSWarner Losh 			/*
566d0aaeffdSWarner Losh 			 * Here we know that we need to zero cq_head and then negate
567d0aaeffdSWarner Losh 			 * the phase, which hasn't been assigned if cq_head isn't
568d0aaeffdSWarner Losh 			 * zero due to the atomic_store_rel.
569d0aaeffdSWarner Losh 			 */
570d0aaeffdSWarner Losh 			qpair->cq_head = 0;
571d0aaeffdSWarner Losh 			qpair->phase = !qpair->phase;
572d0aaeffdSWarner Losh 		} else if (qpair->cq_head == 0) {
573d0aaeffdSWarner Losh 			/*
574d0aaeffdSWarner Losh 			 * In this case, we know that the assignment at 2
575d0aaeffdSWarner Losh 			 * happened below, but we don't know if it 3 happened or
576d0aaeffdSWarner Losh 			 * not. To do this, we look at the last completion
577d0aaeffdSWarner Losh 			 * entry and set the phase to the opposite phase
578d0aaeffdSWarner Losh 			 * that it has. This gets us back in sync
579d0aaeffdSWarner Losh 			 */
580d0aaeffdSWarner Losh 			cpl = qpair->cpl[qpair->num_entries - 1];
581d0aaeffdSWarner Losh 			nvme_completion_swapbytes(&cpl);
582d0aaeffdSWarner Losh 			qpair->phase = !NVME_STATUS_GET_P(cpl.status);
583d0aaeffdSWarner Losh 		}
584d0aaeffdSWarner Losh 	}
585d0aaeffdSWarner Losh 
5862e0090afSJustin Hibbits 	bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
5872e0090afSJustin Hibbits 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
5886cb06070SJim Harris 	while (1) {
5890d787e9bSWojciech Macek 		cpl = qpair->cpl[qpair->cq_head];
5906cb06070SJim Harris 
5910d787e9bSWojciech Macek 		/* Convert to host endian */
5920d787e9bSWojciech Macek 		nvme_completion_swapbytes(&cpl);
5930d787e9bSWojciech Macek 
5940d787e9bSWojciech Macek 		if (NVME_STATUS_GET_P(cpl.status) != qpair->phase)
5956cb06070SJim Harris 			break;
5966cb06070SJim Harris 
5970d787e9bSWojciech Macek 		tr = qpair->act_tr[cpl.cid];
5986cb06070SJim Harris 
5996cb06070SJim Harris 		if (tr != NULL) {
60043393e8bSWarner Losh 			nvme_qpair_complete_tracker(tr, &cpl, ERROR_PRINT_ALL);
6010d787e9bSWojciech Macek 			qpair->sq_head = cpl.sqhd;
602d85d9648SWarner Losh 			done++;
603d0aaeffdSWarner Losh 		} else if (!in_panic) {
604d0aaeffdSWarner Losh 			/*
605d0aaeffdSWarner Losh 			 * A missing tracker is normally an error.  However, a
606d0aaeffdSWarner Losh 			 * panic can stop the CPU this routine is running on
607d0aaeffdSWarner Losh 			 * after completing an I/O but before updating
608d0aaeffdSWarner Losh 			 * qpair->cq_head at 1 below.  Later, we re-enter this
609d0aaeffdSWarner Losh 			 * routine to poll I/O associated with the kernel
610d0aaeffdSWarner Losh 			 * dump. We find that the tr has been set to null before
611d0aaeffdSWarner Losh 			 * calling the completion routine.  If it hasn't
612d0aaeffdSWarner Losh 			 * completed (or it triggers a panic), then '1' below
613d0aaeffdSWarner Losh 			 * won't have updated cq_head. Rather than panic again,
614d0aaeffdSWarner Losh 			 * ignore this condition because it's not unexpected.
615d0aaeffdSWarner Losh 			 */
616547d523eSJim Harris 			nvme_printf(qpair->ctrlr,
617547d523eSJim Harris 			    "cpl does not map to outstanding cmd\n");
6180d787e9bSWojciech Macek 			/* nvme_dump_completion expects device endianess */
6190d787e9bSWojciech Macek 			nvme_dump_completion(&qpair->cpl[qpair->cq_head]);
620d0aaeffdSWarner Losh 			KASSERT(0, ("received completion for unknown cmd"));
6216cb06070SJim Harris 		}
622bb0ec6b3SJim Harris 
623d0aaeffdSWarner Losh 		/*
624d0aaeffdSWarner Losh 		 * There's a number of races with the following (see above) when
625d0aaeffdSWarner Losh 		 * the system panics. We compensate for each one of them by
626d0aaeffdSWarner Losh 		 * using the atomic store to force strong ordering (at least when
627d0aaeffdSWarner Losh 		 * viewed in the aftermath of a panic).
628d0aaeffdSWarner Losh 		 */
629d0aaeffdSWarner Losh 		if (++qpair->cq_head == qpair->num_entries) {		/* 1 */
630d0aaeffdSWarner Losh 			atomic_store_rel_int(&qpair->cq_head, 0);	/* 2 */
631d0aaeffdSWarner Losh 			qpair->phase = !qpair->phase;			/* 3 */
632bb0ec6b3SJim Harris 		}
633bb0ec6b3SJim Harris 
634f93b7f95SWarner Losh 		bus_space_write_4(qpair->ctrlr->bus_tag, qpair->ctrlr->bus_handle,
635f93b7f95SWarner Losh 		    qpair->cq_hdbl_off, qpair->cq_head);
636bb0ec6b3SJim Harris 	}
637d85d9648SWarner Losh 	return (done != 0);
638bb0ec6b3SJim Harris }
639bb0ec6b3SJim Harris 
640bb0ec6b3SJim Harris static void
641bb0ec6b3SJim Harris nvme_qpair_msix_handler(void *arg)
642bb0ec6b3SJim Harris {
643bb0ec6b3SJim Harris 	struct nvme_qpair *qpair = arg;
644bb0ec6b3SJim Harris 
645bb0ec6b3SJim Harris 	nvme_qpair_process_completions(qpair);
646bb0ec6b3SJim Harris }
647bb0ec6b3SJim Harris 
648a965389bSScott Long int
6491eab19cbSAlexander Motin nvme_qpair_construct(struct nvme_qpair *qpair,
6501eab19cbSAlexander Motin     uint32_t num_entries, uint32_t num_trackers,
6518d09e3c4SJim Harris     struct nvme_controller *ctrlr)
652bb0ec6b3SJim Harris {
65321b6da58SJim Harris 	struct nvme_tracker	*tr;
654a965389bSScott Long 	size_t			cmdsz, cplsz, prpsz, allocsz, prpmemsz;
655a965389bSScott Long 	uint64_t		queuemem_phys, prpmem_phys, list_phys;
656a965389bSScott Long 	uint8_t			*queuemem, *prpmem, *prp_list;
657a965389bSScott Long 	int			i, err;
658bb0ec6b3SJim Harris 
6591eab19cbSAlexander Motin 	qpair->vector = ctrlr->msix_enabled ? qpair->id : 0;
660bb0ec6b3SJim Harris 	qpair->num_entries = num_entries;
6610f71ecf7SJim Harris 	qpair->num_trackers = num_trackers;
662bb0ec6b3SJim Harris 	qpair->ctrlr = ctrlr;
663bb0ec6b3SJim Harris 
664bb0ec6b3SJim Harris 	if (ctrlr->msix_enabled) {
665bb0ec6b3SJim Harris 
666bb0ec6b3SJim Harris 		/*
667bb0ec6b3SJim Harris 		 * MSI-X vector resource IDs start at 1, so we add one to
668bb0ec6b3SJim Harris 		 *  the queue's vector to get the corresponding rid to use.
669bb0ec6b3SJim Harris 		 */
6701eab19cbSAlexander Motin 		qpair->rid = qpair->vector + 1;
671bb0ec6b3SJim Harris 
672e5af5854SJim Harris 		qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
673e5af5854SJim Harris 		    &qpair->rid, RF_ACTIVE);
6744053f8acSDavid Bright 		if (bus_setup_intr(ctrlr->dev, qpair->res,
675bb0ec6b3SJim Harris 		    INTR_TYPE_MISC | INTR_MPSAFE, NULL,
6764053f8acSDavid Bright 		    nvme_qpair_msix_handler, qpair, &qpair->tag) != 0) {
6774053f8acSDavid Bright 			nvme_printf(ctrlr, "unable to setup intx handler\n");
6784053f8acSDavid Bright 			goto out;
6794053f8acSDavid Bright 		}
6801eab19cbSAlexander Motin 		if (qpair->id == 0) {
681a6461357SAlexander Motin 			bus_describe_intr(ctrlr->dev, qpair->res, qpair->tag,
682a6461357SAlexander Motin 			    "admin");
683a6461357SAlexander Motin 		} else {
684a6461357SAlexander Motin 			bus_describe_intr(ctrlr->dev, qpair->res, qpair->tag,
6851eab19cbSAlexander Motin 			    "io%d", qpair->id - 1);
686a6461357SAlexander Motin 		}
687bb0ec6b3SJim Harris 	}
688bb0ec6b3SJim Harris 
689bb0ec6b3SJim Harris 	mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF);
690bb0ec6b3SJim Harris 
6911416ef36SJim Harris 	/* Note: NVMe PRP format is restricted to 4-byte alignment. */
692a6e30963SJim Harris 	err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
6931416ef36SJim Harris 	    4, PAGE_SIZE, BUS_SPACE_MAXADDR,
6948d09e3c4SJim Harris 	    BUS_SPACE_MAXADDR, NULL, NULL, NVME_MAX_XFER_SIZE,
6958d09e3c4SJim Harris 	    (NVME_MAX_XFER_SIZE/PAGE_SIZE)+1, PAGE_SIZE, 0,
696a6e30963SJim Harris 	    NULL, NULL, &qpair->dma_tag_payload);
697a965389bSScott Long 	if (err != 0) {
698a6e30963SJim Harris 		nvme_printf(ctrlr, "payload tag create failed %d\n", err);
699a965389bSScott Long 		goto out;
700a965389bSScott Long 	}
701a965389bSScott Long 
702a965389bSScott Long 	/*
703a965389bSScott Long 	 * Each component must be page aligned, and individual PRP lists
704a965389bSScott Long 	 * cannot cross a page boundary.
705a965389bSScott Long 	 */
706a965389bSScott Long 	cmdsz = qpair->num_entries * sizeof(struct nvme_command);
707a965389bSScott Long 	cmdsz = roundup2(cmdsz, PAGE_SIZE);
708a965389bSScott Long 	cplsz = qpair->num_entries * sizeof(struct nvme_completion);
709a965389bSScott Long 	cplsz = roundup2(cplsz, PAGE_SIZE);
710aeb665b5SEd Maste 	prpsz = sizeof(uint64_t) * NVME_MAX_PRP_LIST_ENTRIES;
711a965389bSScott Long 	prpmemsz = qpair->num_trackers * prpsz;
712a965389bSScott Long 	allocsz = cmdsz + cplsz + prpmemsz;
713a6e30963SJim Harris 
714a6e30963SJim Harris 	err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
715a965389bSScott Long 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
716a965389bSScott Long 	    allocsz, 1, allocsz, 0, NULL, NULL, &qpair->dma_tag);
717a965389bSScott Long 	if (err != 0) {
718a6e30963SJim Harris 		nvme_printf(ctrlr, "tag create failed %d\n", err);
719a965389bSScott Long 		goto out;
720a965389bSScott Long 	}
7211eab19cbSAlexander Motin 	bus_dma_tag_set_domain(qpair->dma_tag, qpair->domain);
722a965389bSScott Long 
723a965389bSScott Long 	if (bus_dmamem_alloc(qpair->dma_tag, (void **)&queuemem,
724a965389bSScott Long 	    BUS_DMA_NOWAIT, &qpair->queuemem_map)) {
725a965389bSScott Long 		nvme_printf(ctrlr, "failed to alloc qpair memory\n");
726a965389bSScott Long 		goto out;
727a965389bSScott Long 	}
728a965389bSScott Long 
729a965389bSScott Long 	if (bus_dmamap_load(qpair->dma_tag, qpair->queuemem_map,
730a965389bSScott Long 	    queuemem, allocsz, nvme_single_map, &queuemem_phys, 0) != 0) {
731a965389bSScott Long 		nvme_printf(ctrlr, "failed to load qpair memory\n");
732550d5d64SAlexander Motin 		bus_dmamem_free(qpair->dma_tag, qpair->cmd,
733550d5d64SAlexander Motin 		    qpair->queuemem_map);
734a965389bSScott Long 		goto out;
735a965389bSScott Long 	}
736bb0ec6b3SJim Harris 
737bb0ec6b3SJim Harris 	qpair->num_cmds = 0;
7386568ebfcSJim Harris 	qpair->num_intr_handler_calls = 0;
739c37fc318SWarner Losh 	qpair->num_retries = 0;
7405e83c2ffSWarner Losh 	qpair->num_failures = 0;
741a965389bSScott Long 	qpair->cmd = (struct nvme_command *)queuemem;
742a965389bSScott Long 	qpair->cpl = (struct nvme_completion *)(queuemem + cmdsz);
743a965389bSScott Long 	prpmem = (uint8_t *)(queuemem + cmdsz + cplsz);
744a965389bSScott Long 	qpair->cmd_bus_addr = queuemem_phys;
745a965389bSScott Long 	qpair->cpl_bus_addr = queuemem_phys + cmdsz;
746a965389bSScott Long 	prpmem_phys = queuemem_phys + cmdsz + cplsz;
747bb0ec6b3SJim Harris 
748f93b7f95SWarner Losh 	/*
749f93b7f95SWarner Losh 	 * Calcuate the stride of the doorbell register. Many emulators set this
750f93b7f95SWarner Losh 	 * value to correspond to a cache line. However, some hardware has set
751f93b7f95SWarner Losh 	 * it to various small values.
752f93b7f95SWarner Losh 	 */
753f93b7f95SWarner Losh 	qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[0]) +
7541eab19cbSAlexander Motin 	    (qpair->id << (ctrlr->dstrd + 1));
755f93b7f95SWarner Losh 	qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[0]) +
7561eab19cbSAlexander Motin 	    (qpair->id << (ctrlr->dstrd + 1)) + (1 << ctrlr->dstrd);
757bb0ec6b3SJim Harris 
75865c2474eSJim Harris 	TAILQ_INIT(&qpair->free_tr);
75965c2474eSJim Harris 	TAILQ_INIT(&qpair->outstanding_tr);
7600f71ecf7SJim Harris 	STAILQ_INIT(&qpair->queued_req);
761bb0ec6b3SJim Harris 
762a965389bSScott Long 	list_phys = prpmem_phys;
763a965389bSScott Long 	prp_list = prpmem;
7640f71ecf7SJim Harris 	for (i = 0; i < qpair->num_trackers; i++) {
765a965389bSScott Long 
766a965389bSScott Long 		if (list_phys + prpsz > prpmem_phys + prpmemsz) {
767a965389bSScott Long 			qpair->num_trackers = i;
768a965389bSScott Long 			break;
76921b6da58SJim Harris 		}
77021b6da58SJim Harris 
771a965389bSScott Long 		/*
772a965389bSScott Long 		 * Make sure that the PRP list for this tracker doesn't
773a965389bSScott Long 		 * overflow to another page.
774a965389bSScott Long 		 */
775a965389bSScott Long 		if (trunc_page(list_phys) !=
776a965389bSScott Long 		    trunc_page(list_phys + prpsz - 1)) {
777a965389bSScott Long 			list_phys = roundup2(list_phys, PAGE_SIZE);
778a965389bSScott Long 			prp_list =
779a965389bSScott Long 			    (uint8_t *)roundup2((uintptr_t)prp_list, PAGE_SIZE);
780a965389bSScott Long 		}
781a965389bSScott Long 
7821eab19cbSAlexander Motin 		tr = malloc_domainset(sizeof(*tr), M_NVME,
7831eab19cbSAlexander Motin 		    DOMAINSET_PREF(qpair->domain), M_ZERO | M_WAITOK);
784a965389bSScott Long 		bus_dmamap_create(qpair->dma_tag_payload, 0,
785a965389bSScott Long 		    &tr->payload_dma_map);
786a965389bSScott Long 		callout_init(&tr->timer, 1);
787a965389bSScott Long 		tr->cid = i;
788a965389bSScott Long 		tr->qpair = qpair;
789a965389bSScott Long 		tr->prp = (uint64_t *)prp_list;
790a965389bSScott Long 		tr->prp_bus_addr = list_phys;
791a965389bSScott Long 		TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
792a965389bSScott Long 		list_phys += prpsz;
793a965389bSScott Long 		prp_list += prpsz;
794a965389bSScott Long 	}
795a965389bSScott Long 
796a965389bSScott Long 	if (qpair->num_trackers == 0) {
797a965389bSScott Long 		nvme_printf(ctrlr, "failed to allocate enough trackers\n");
798a965389bSScott Long 		goto out;
799a965389bSScott Long 	}
800a965389bSScott Long 
8011eab19cbSAlexander Motin 	qpair->act_tr = malloc_domainset(sizeof(struct nvme_tracker *) *
8021eab19cbSAlexander Motin 	    qpair->num_entries, M_NVME, DOMAINSET_PREF(qpair->domain),
8031eab19cbSAlexander Motin 	    M_ZERO | M_WAITOK);
804a965389bSScott Long 	return (0);
805a965389bSScott Long 
806a965389bSScott Long out:
807a965389bSScott Long 	nvme_qpair_destroy(qpair);
808a965389bSScott Long 	return (ENOMEM);
809bb0ec6b3SJim Harris }
810bb0ec6b3SJim Harris 
811bb0ec6b3SJim Harris static void
812bb0ec6b3SJim Harris nvme_qpair_destroy(struct nvme_qpair *qpair)
813bb0ec6b3SJim Harris {
814bb0ec6b3SJim Harris 	struct nvme_tracker	*tr;
815bb0ec6b3SJim Harris 
816550d5d64SAlexander Motin 	if (qpair->tag) {
817bb0ec6b3SJim Harris 		bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag);
818550d5d64SAlexander Motin 		qpair->tag = NULL;
8193d7eb41cSJim Harris 	}
8203d7eb41cSJim Harris 
821550d5d64SAlexander Motin 	if (qpair->act_tr) {
822*96ad26eeSMark Johnston 		free(qpair->act_tr, M_NVME);
823550d5d64SAlexander Motin 		qpair->act_tr = NULL;
824550d5d64SAlexander Motin 	}
825bb0ec6b3SJim Harris 
82665c2474eSJim Harris 	while (!TAILQ_EMPTY(&qpair->free_tr)) {
82765c2474eSJim Harris 		tr = TAILQ_FIRST(&qpair->free_tr);
82865c2474eSJim Harris 		TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
829c6c70c07SAlexander Motin 		bus_dmamap_destroy(qpair->dma_tag_payload,
830c6c70c07SAlexander Motin 		    tr->payload_dma_map);
831*96ad26eeSMark Johnston 		free(tr, M_NVME);
832bb0ec6b3SJim Harris 	}
833c6c70c07SAlexander Motin 
834550d5d64SAlexander Motin 	if (qpair->cmd != NULL) {
835550d5d64SAlexander Motin 		bus_dmamap_unload(qpair->dma_tag, qpair->queuemem_map);
836550d5d64SAlexander Motin 		bus_dmamem_free(qpair->dma_tag, qpair->cmd,
837550d5d64SAlexander Motin 		    qpair->queuemem_map);
838550d5d64SAlexander Motin 		qpair->cmd = NULL;
839550d5d64SAlexander Motin 	}
840c6c70c07SAlexander Motin 
841550d5d64SAlexander Motin 	if (qpair->dma_tag) {
842550d5d64SAlexander Motin 		bus_dma_tag_destroy(qpair->dma_tag);
843550d5d64SAlexander Motin 		qpair->dma_tag = NULL;
844550d5d64SAlexander Motin 	}
845550d5d64SAlexander Motin 
846550d5d64SAlexander Motin 	if (qpair->dma_tag_payload) {
847c6c70c07SAlexander Motin 		bus_dma_tag_destroy(qpair->dma_tag_payload);
848550d5d64SAlexander Motin 		qpair->dma_tag_payload = NULL;
849550d5d64SAlexander Motin 	}
850550d5d64SAlexander Motin 
851550d5d64SAlexander Motin 	if (mtx_initialized(&qpair->lock))
852550d5d64SAlexander Motin 		mtx_destroy(&qpair->lock);
853550d5d64SAlexander Motin 
854550d5d64SAlexander Motin 	if (qpair->res) {
855550d5d64SAlexander Motin 		bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ,
856550d5d64SAlexander Motin 		    rman_get_rid(qpair->res), qpair->res);
857550d5d64SAlexander Motin 		qpair->res = NULL;
858550d5d64SAlexander Motin 	}
859bb0ec6b3SJim Harris }
860bb0ec6b3SJim Harris 
861b846efd7SJim Harris static void
862b846efd7SJim Harris nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair)
863b846efd7SJim Harris {
864b846efd7SJim Harris 	struct nvme_tracker	*tr;
865b846efd7SJim Harris 
866b846efd7SJim Harris 	tr = TAILQ_FIRST(&qpair->outstanding_tr);
867b846efd7SJim Harris 	while (tr != NULL) {
8689544e6dcSChuck Tuffli 		if (tr->req->cmd.opc == NVME_OPC_ASYNC_EVENT_REQUEST) {
86943393e8bSWarner Losh 			nvme_qpair_manual_complete_tracker(tr,
870232e2edbSJim Harris 			    NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0,
8712ffd6fceSWarner Losh 			    ERROR_PRINT_NONE);
872b846efd7SJim Harris 			tr = TAILQ_FIRST(&qpair->outstanding_tr);
873b846efd7SJim Harris 		} else {
874b846efd7SJim Harris 			tr = TAILQ_NEXT(tr, tailq);
875b846efd7SJim Harris 		}
876b846efd7SJim Harris 	}
877b846efd7SJim Harris }
878b846efd7SJim Harris 
879bb0ec6b3SJim Harris void
880bb0ec6b3SJim Harris nvme_admin_qpair_destroy(struct nvme_qpair *qpair)
881bb0ec6b3SJim Harris {
882bb0ec6b3SJim Harris 
883b846efd7SJim Harris 	nvme_admin_qpair_abort_aers(qpair);
884bb0ec6b3SJim Harris 	nvme_qpair_destroy(qpair);
885bb0ec6b3SJim Harris }
886bb0ec6b3SJim Harris 
887bb0ec6b3SJim Harris void
888bb0ec6b3SJim Harris nvme_io_qpair_destroy(struct nvme_qpair *qpair)
889bb0ec6b3SJim Harris {
890bb0ec6b3SJim Harris 
891bb0ec6b3SJim Harris 	nvme_qpair_destroy(qpair);
892bb0ec6b3SJim Harris }
893bb0ec6b3SJim Harris 
894bb0ec6b3SJim Harris static void
8950a0b08ccSJim Harris nvme_abort_complete(void *arg, const struct nvme_completion *status)
8960a0b08ccSJim Harris {
897879de699SJim Harris 	struct nvme_tracker	*tr = arg;
898879de699SJim Harris 
899879de699SJim Harris 	/*
900879de699SJim Harris 	 * If cdw0 == 1, the controller was not able to abort the command
901879de699SJim Harris 	 *  we requested.  We still need to check the active tracker array,
902879de699SJim Harris 	 *  to cover race where I/O timed out at same time controller was
903879de699SJim Harris 	 *  completing the I/O.
904879de699SJim Harris 	 */
905879de699SJim Harris 	if (status->cdw0 == 1 && tr->qpair->act_tr[tr->cid] != NULL) {
906879de699SJim Harris 		/*
907879de699SJim Harris 		 * An I/O has timed out, and the controller was unable to
908879de699SJim Harris 		 *  abort it for some reason.  Construct a fake completion
909879de699SJim Harris 		 *  status, and then complete the I/O's tracker manually.
910879de699SJim Harris 		 */
911547d523eSJim Harris 		nvme_printf(tr->qpair->ctrlr,
912547d523eSJim Harris 		    "abort command failed, aborting command manually\n");
91343393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr,
9142ffd6fceSWarner Losh 		    NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 0, ERROR_PRINT_ALL);
915879de699SJim Harris 	}
916879de699SJim Harris }
917879de699SJim Harris 
918879de699SJim Harris static void
919bb0ec6b3SJim Harris nvme_timeout(void *arg)
920bb0ec6b3SJim Harris {
921448195e7SJim Harris 	struct nvme_tracker	*tr = arg;
92212d191ecSJim Harris 	struct nvme_qpair	*qpair = tr->qpair;
92312d191ecSJim Harris 	struct nvme_controller	*ctrlr = qpair->ctrlr;
9240d787e9bSWojciech Macek 	uint32_t		csts;
9250d787e9bSWojciech Macek 	uint8_t			cfs;
926448195e7SJim Harris 
92748ce3178SJim Harris 	/*
928d85d9648SWarner Losh 	 * Read csts to get value of cfs - controller fatal status.
929d85d9648SWarner Losh 	 * If no fatal status, try to call the completion routine, and
930d85d9648SWarner Losh 	 * if completes transactions, report a missed interrupt and
931d85d9648SWarner Losh 	 * return (this may need to be rate limited). Otherwise, if
932d85d9648SWarner Losh 	 * aborts are enabled and the controller is not reporting
933d85d9648SWarner Losh 	 * fatal status, abort the command. Otherwise, just reset the
934d85d9648SWarner Losh 	 * controller and hope for the best.
93548ce3178SJim Harris 	 */
936d85d9648SWarner Losh 	csts = nvme_mmio_read_4(ctrlr, csts);
937d85d9648SWarner Losh 	cfs = (csts >> NVME_CSTS_REG_CFS_SHIFT) & NVME_CSTS_REG_CFS_MASK;
938d85d9648SWarner Losh 	if (cfs == 0 && nvme_qpair_process_completions(qpair)) {
939d85d9648SWarner Losh 		nvme_printf(ctrlr, "Missing interrupt\n");
940d85d9648SWarner Losh 		return;
941d85d9648SWarner Losh 	}
942d85d9648SWarner Losh 	if (ctrlr->enable_aborts && cfs == 0) {
943d85d9648SWarner Losh 		nvme_printf(ctrlr, "Aborting command due to a timeout.\n");
94412d191ecSJim Harris 		nvme_ctrlr_cmd_abort(ctrlr, tr->cid, qpair->id,
945879de699SJim Harris 		    nvme_abort_complete, tr);
946d85d9648SWarner Losh 	} else {
947d85d9648SWarner Losh 		nvme_printf(ctrlr, "Resetting controller due to a timeout%s.\n",
94871a28181SAlexander Motin 		    (csts == 0xffffffff) ? " and possible hot unplug" :
94971a28181SAlexander Motin 		    (cfs ? " and fatal error status" : ""));
95048ce3178SJim Harris 		nvme_ctrlr_reset(ctrlr);
951bb0ec6b3SJim Harris 	}
952d85d9648SWarner Losh }
953bb0ec6b3SJim Harris 
954bb0ec6b3SJim Harris void
955b846efd7SJim Harris nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr)
956bb0ec6b3SJim Harris {
957ad697276SJim Harris 	struct nvme_request	*req;
95894143332SJim Harris 	struct nvme_controller	*ctrlr;
959ead7e103SAlexander Motin 	int timeout;
960bb0ec6b3SJim Harris 
961b846efd7SJim Harris 	mtx_assert(&qpair->lock, MA_OWNED);
962b846efd7SJim Harris 
963ad697276SJim Harris 	req = tr->req;
964ad697276SJim Harris 	req->cmd.cid = tr->cid;
965bb0ec6b3SJim Harris 	qpair->act_tr[tr->cid] = tr;
96694143332SJim Harris 	ctrlr = qpair->ctrlr;
967bb0ec6b3SJim Harris 
968ead7e103SAlexander Motin 	if (req->timeout) {
969ead7e103SAlexander Motin 		if (req->cb_fn == nvme_completion_poll_cb)
970ead7e103SAlexander Motin 			timeout = hz;
971ead7e103SAlexander Motin 		else
972ead7e103SAlexander Motin 			timeout = ctrlr->timeout_period * hz;
973ead7e103SAlexander Motin 		callout_reset_on(&tr->timer, timeout, nvme_timeout, tr,
974ead7e103SAlexander Motin 		    qpair->cpu);
975ead7e103SAlexander Motin 	}
976bb0ec6b3SJim Harris 
977bb0ec6b3SJim Harris 	/* Copy the command from the tracker to the submission queue. */
978ad697276SJim Harris 	memcpy(&qpair->cmd[qpair->sq_tail], &req->cmd, sizeof(req->cmd));
979bb0ec6b3SJim Harris 
980bb0ec6b3SJim Harris 	if (++qpair->sq_tail == qpair->num_entries)
981bb0ec6b3SJim Harris 		qpair->sq_tail = 0;
982bb0ec6b3SJim Harris 
9832e0090afSJustin Hibbits 	bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
9842e0090afSJustin Hibbits 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
9852e0090afSJustin Hibbits #ifndef __powerpc__
9862e0090afSJustin Hibbits 	/*
9872e0090afSJustin Hibbits 	 * powerpc's bus_dmamap_sync() already includes a heavyweight sync, but
9882e0090afSJustin Hibbits 	 * no other archs do.
9892e0090afSJustin Hibbits 	 */
990bb0ec6b3SJim Harris 	wmb();
9912e0090afSJustin Hibbits #endif
9922e0090afSJustin Hibbits 
993f93b7f95SWarner Losh 	bus_space_write_4(qpair->ctrlr->bus_tag, qpair->ctrlr->bus_handle,
994f93b7f95SWarner Losh 	    qpair->sq_tdbl_off, qpair->sq_tail);
995bb0ec6b3SJim Harris 	qpair->num_cmds++;
996bb0ec6b3SJim Harris }
9975ae9ed68SJim Harris 
998d6f54866SJim Harris static void
999ca269f32SJim Harris nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1000ca269f32SJim Harris {
1001ca269f32SJim Harris 	struct nvme_tracker 	*tr = arg;
1002ca269f32SJim Harris 	uint32_t		cur_nseg;
1003ca269f32SJim Harris 
1004ca269f32SJim Harris 	/*
1005ca269f32SJim Harris 	 * If the mapping operation failed, return immediately.  The caller
1006ca269f32SJim Harris 	 *  is responsible for detecting the error status and failing the
1007ca269f32SJim Harris 	 *  tracker manually.
1008ca269f32SJim Harris 	 */
1009a6e30963SJim Harris 	if (error != 0) {
1010a6e30963SJim Harris 		nvme_printf(tr->qpair->ctrlr,
1011a6e30963SJim Harris 		    "nvme_payload_map err %d\n", error);
1012ca269f32SJim Harris 		return;
1013a6e30963SJim Harris 	}
1014ca269f32SJim Harris 
1015ca269f32SJim Harris 	/*
1016ca269f32SJim Harris 	 * Note that we specified PAGE_SIZE for alignment and max
1017ca269f32SJim Harris 	 *  segment size when creating the bus dma tags.  So here
1018ca269f32SJim Harris 	 *  we can safely just transfer each segment to its
1019ca269f32SJim Harris 	 *  associated PRP entry.
1020ca269f32SJim Harris 	 */
10210d787e9bSWojciech Macek 	tr->req->cmd.prp1 = htole64(seg[0].ds_addr);
1022ca269f32SJim Harris 
1023ca269f32SJim Harris 	if (nseg == 2) {
10240d787e9bSWojciech Macek 		tr->req->cmd.prp2 = htole64(seg[1].ds_addr);
1025ca269f32SJim Harris 	} else if (nseg > 2) {
1026ca269f32SJim Harris 		cur_nseg = 1;
10270d787e9bSWojciech Macek 		tr->req->cmd.prp2 = htole64((uint64_t)tr->prp_bus_addr);
1028ca269f32SJim Harris 		while (cur_nseg < nseg) {
1029ca269f32SJim Harris 			tr->prp[cur_nseg-1] =
10300d787e9bSWojciech Macek 			    htole64((uint64_t)seg[cur_nseg].ds_addr);
1031ca269f32SJim Harris 			cur_nseg++;
1032ca269f32SJim Harris 		}
1033a6e30963SJim Harris 	} else {
1034a6e30963SJim Harris 		/*
1035a6e30963SJim Harris 		 * prp2 should not be used by the controller
1036a6e30963SJim Harris 		 *  since there is only one segment, but set
1037a6e30963SJim Harris 		 *  to 0 just to be safe.
1038a6e30963SJim Harris 		 */
1039a6e30963SJim Harris 		tr->req->cmd.prp2 = 0;
1040ca269f32SJim Harris 	}
1041ca269f32SJim Harris 
10422e0090afSJustin Hibbits 	bus_dmamap_sync(tr->qpair->dma_tag_payload, tr->payload_dma_map,
10432e0090afSJustin Hibbits 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1044ca269f32SJim Harris 	nvme_qpair_submit_tracker(tr->qpair, tr);
1045ca269f32SJim Harris }
1046ca269f32SJim Harris 
1047ca269f32SJim Harris static void
1048d6f54866SJim Harris _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
10495ae9ed68SJim Harris {
10505ae9ed68SJim Harris 	struct nvme_tracker	*tr;
1051e2b99004SJim Harris 	int			err = 0;
10525ae9ed68SJim Harris 
1053d6f54866SJim Harris 	mtx_assert(&qpair->lock, MA_OWNED);
10545ae9ed68SJim Harris 
105565c2474eSJim Harris 	tr = TAILQ_FIRST(&qpair->free_tr);
1056232e2edbSJim Harris 	req->qpair = qpair;
105721b6da58SJim Harris 
1058b846efd7SJim Harris 	if (tr == NULL || !qpair->is_enabled) {
10590f71ecf7SJim Harris 		/*
1060b846efd7SJim Harris 		 * No tracker is available, or the qpair is disabled due to
1061232e2edbSJim Harris 		 *  an in-progress controller-level reset or controller
1062232e2edbSJim Harris 		 *  failure.
1063232e2edbSJim Harris 		 */
1064232e2edbSJim Harris 
1065232e2edbSJim Harris 		if (qpair->ctrlr->is_failed) {
1066232e2edbSJim Harris 			/*
1067232e2edbSJim Harris 			 * The controller has failed.  Post the request to a
1068232e2edbSJim Harris 			 *  task where it will be aborted, so that we do not
1069232e2edbSJim Harris 			 *  invoke the request's callback in the context
1070232e2edbSJim Harris 			 *  of the submission.
1071232e2edbSJim Harris 			 */
1072232e2edbSJim Harris 			nvme_ctrlr_post_failed_request(qpair->ctrlr, req);
1073232e2edbSJim Harris 		} else {
1074232e2edbSJim Harris 			/*
1075232e2edbSJim Harris 			 * Put the request on the qpair's request queue to be
1076232e2edbSJim Harris 			 *  processed when a tracker frees up via a command
1077232e2edbSJim Harris 			 *  completion or when the controller reset is
1078232e2edbSJim Harris 			 *  completed.
10790f71ecf7SJim Harris 			 */
10800f71ecf7SJim Harris 			STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq);
1081232e2edbSJim Harris 		}
1082d6f54866SJim Harris 		return;
108321b6da58SJim Harris 	}
108421b6da58SJim Harris 
108565c2474eSJim Harris 	TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
108665c2474eSJim Harris 	TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq);
10875ae9ed68SJim Harris 	tr->req = req;
10885ae9ed68SJim Harris 
10891e526bc4SJim Harris 	switch (req->type) {
10901e526bc4SJim Harris 	case NVME_REQUEST_VADDR:
10917b68ae1eSJim Harris 		KASSERT(req->payload_size <= qpair->ctrlr->max_xfer_size,
10927b68ae1eSJim Harris 		    ("payload_size (%d) exceeds max_xfer_size (%d)\n",
10937b68ae1eSJim Harris 		    req->payload_size, qpair->ctrlr->max_xfer_size));
1094a6e30963SJim Harris 		err = bus_dmamap_load(tr->qpair->dma_tag_payload,
1095a6e30963SJim Harris 		    tr->payload_dma_map, req->u.payload, req->payload_size,
1096a6e30963SJim Harris 		    nvme_payload_map, tr, 0);
10975ae9ed68SJim Harris 		if (err != 0)
1098e2b99004SJim Harris 			nvme_printf(qpair->ctrlr,
1099e2b99004SJim Harris 			    "bus_dmamap_load returned 0x%x!\n", err);
11001e526bc4SJim Harris 		break;
11011e526bc4SJim Harris 	case NVME_REQUEST_NULL:
1102b846efd7SJim Harris 		nvme_qpair_submit_tracker(tr->qpair, tr);
11031e526bc4SJim Harris 		break;
11045fdf9c3cSJim Harris 	case NVME_REQUEST_BIO:
11057b68ae1eSJim Harris 		KASSERT(req->u.bio->bio_bcount <= qpair->ctrlr->max_xfer_size,
11067b68ae1eSJim Harris 		    ("bio->bio_bcount (%jd) exceeds max_xfer_size (%d)\n",
11077b68ae1eSJim Harris 		    (intmax_t)req->u.bio->bio_bcount,
11087b68ae1eSJim Harris 		    qpair->ctrlr->max_xfer_size));
1109a6e30963SJim Harris 		err = bus_dmamap_load_bio(tr->qpair->dma_tag_payload,
11105fdf9c3cSJim Harris 		    tr->payload_dma_map, req->u.bio, nvme_payload_map, tr, 0);
11115fdf9c3cSJim Harris 		if (err != 0)
1112e2b99004SJim Harris 			nvme_printf(qpair->ctrlr,
1113e2b99004SJim Harris 			    "bus_dmamap_load_bio returned 0x%x!\n", err);
11145fdf9c3cSJim Harris 		break;
111551977281SWarner Losh 	case NVME_REQUEST_CCB:
111651977281SWarner Losh 		err = bus_dmamap_load_ccb(tr->qpair->dma_tag_payload,
111751977281SWarner Losh 		    tr->payload_dma_map, req->u.payload,
111851977281SWarner Losh 		    nvme_payload_map, tr, 0);
111951977281SWarner Losh 		if (err != 0)
112051977281SWarner Losh 			nvme_printf(qpair->ctrlr,
112151977281SWarner Losh 			    "bus_dmamap_load_ccb returned 0x%x!\n", err);
112251977281SWarner Losh 		break;
11231e526bc4SJim Harris 	default:
11241e526bc4SJim Harris 		panic("unknown nvme request type 0x%x\n", req->type);
11251e526bc4SJim Harris 		break;
11265ae9ed68SJim Harris 	}
1127e2b99004SJim Harris 
1128e2b99004SJim Harris 	if (err != 0) {
1129e2b99004SJim Harris 		/*
1130e2b99004SJim Harris 		 * The dmamap operation failed, so we manually fail the
1131e2b99004SJim Harris 		 *  tracker here with DATA_TRANSFER_ERROR status.
1132e2b99004SJim Harris 		 *
1133e2b99004SJim Harris 		 * nvme_qpair_manual_complete_tracker must not be called
1134e2b99004SJim Harris 		 *  with the qpair lock held.
1135e2b99004SJim Harris 		 */
1136e2b99004SJim Harris 		mtx_unlock(&qpair->lock);
113743393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
11382ffd6fceSWarner Losh 		    NVME_SC_DATA_TRANSFER_ERROR, DO_NOT_RETRY, ERROR_PRINT_ALL);
1139e2b99004SJim Harris 		mtx_lock(&qpair->lock);
1140e2b99004SJim Harris 	}
1141d6f54866SJim Harris }
11425ae9ed68SJim Harris 
1143d6f54866SJim Harris void
1144d6f54866SJim Harris nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
1145d6f54866SJim Harris {
1146d6f54866SJim Harris 
1147d6f54866SJim Harris 	mtx_lock(&qpair->lock);
1148d6f54866SJim Harris 	_nvme_qpair_submit_request(qpair, req);
11495ae9ed68SJim Harris 	mtx_unlock(&qpair->lock);
11505ae9ed68SJim Harris }
1151b846efd7SJim Harris 
1152b846efd7SJim Harris static void
1153b846efd7SJim Harris nvme_qpair_enable(struct nvme_qpair *qpair)
1154b846efd7SJim Harris {
1155b846efd7SJim Harris 
11567588c6ccSWarner Losh 	qpair->is_enabled = true;
1157cb5b7c13SJim Harris }
1158cb5b7c13SJim Harris 
1159cb5b7c13SJim Harris void
1160cb5b7c13SJim Harris nvme_qpair_reset(struct nvme_qpair *qpair)
1161cb5b7c13SJim Harris {
1162cb5b7c13SJim Harris 
1163b846efd7SJim Harris 	qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0;
1164b846efd7SJim Harris 
1165b846efd7SJim Harris 	/*
1166b846efd7SJim Harris 	 * First time through the completion queue, HW will set phase
1167b846efd7SJim Harris 	 *  bit on completions to 1.  So set this to 1 here, indicating
1168b846efd7SJim Harris 	 *  we're looking for a 1 to know which entries have completed.
1169b846efd7SJim Harris 	 *  we'll toggle the bit each time when the completion queue
1170b846efd7SJim Harris 	 *  rolls over.
1171b846efd7SJim Harris 	 */
1172b846efd7SJim Harris 	qpair->phase = 1;
1173b846efd7SJim Harris 
1174b846efd7SJim Harris 	memset(qpair->cmd, 0,
1175b846efd7SJim Harris 	    qpair->num_entries * sizeof(struct nvme_command));
1176b846efd7SJim Harris 	memset(qpair->cpl, 0,
1177b846efd7SJim Harris 	    qpair->num_entries * sizeof(struct nvme_completion));
1178b846efd7SJim Harris }
1179b846efd7SJim Harris 
1180b846efd7SJim Harris void
1181b846efd7SJim Harris nvme_admin_qpair_enable(struct nvme_qpair *qpair)
1182b846efd7SJim Harris {
118343a37256SJim Harris 	struct nvme_tracker		*tr;
118443a37256SJim Harris 	struct nvme_tracker		*tr_temp;
118543a37256SJim Harris 
118643a37256SJim Harris 	/*
118743a37256SJim Harris 	 * Manually abort each outstanding admin command.  Do not retry
118843a37256SJim Harris 	 *  admin commands found here, since they will be left over from
118943a37256SJim Harris 	 *  a controller reset and its likely the context in which the
119043a37256SJim Harris 	 *  command was issued no longer applies.
119143a37256SJim Harris 	 */
119243a37256SJim Harris 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1193547d523eSJim Harris 		nvme_printf(qpair->ctrlr,
119443a37256SJim Harris 		    "aborting outstanding admin command\n");
119543393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
11962ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST, DO_NOT_RETRY, ERROR_PRINT_ALL);
119743a37256SJim Harris 	}
1198b846efd7SJim Harris 
1199b846efd7SJim Harris 	nvme_qpair_enable(qpair);
1200b846efd7SJim Harris }
1201b846efd7SJim Harris 
1202b846efd7SJim Harris void
1203b846efd7SJim Harris nvme_io_qpair_enable(struct nvme_qpair *qpair)
1204b846efd7SJim Harris {
1205b846efd7SJim Harris 	STAILQ_HEAD(, nvme_request)	temp;
1206b846efd7SJim Harris 	struct nvme_tracker		*tr;
1207cb5b7c13SJim Harris 	struct nvme_tracker		*tr_temp;
1208b846efd7SJim Harris 	struct nvme_request		*req;
1209b846efd7SJim Harris 
1210cb5b7c13SJim Harris 	/*
1211cb5b7c13SJim Harris 	 * Manually abort each outstanding I/O.  This normally results in a
1212cb5b7c13SJim Harris 	 *  retry, unless the retry count on the associated request has
1213cb5b7c13SJim Harris 	 *  reached its limit.
1214cb5b7c13SJim Harris 	 */
1215cb5b7c13SJim Harris 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1216547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n");
121743393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
12182ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST, 0, ERROR_PRINT_NO_RETRY);
1219cb5b7c13SJim Harris 	}
1220cb5b7c13SJim Harris 
1221b846efd7SJim Harris 	mtx_lock(&qpair->lock);
1222b846efd7SJim Harris 
1223b846efd7SJim Harris 	nvme_qpair_enable(qpair);
1224b846efd7SJim Harris 
1225b846efd7SJim Harris 	STAILQ_INIT(&temp);
1226b846efd7SJim Harris 	STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request);
1227b846efd7SJim Harris 
1228b846efd7SJim Harris 	while (!STAILQ_EMPTY(&temp)) {
1229b846efd7SJim Harris 		req = STAILQ_FIRST(&temp);
1230b846efd7SJim Harris 		STAILQ_REMOVE_HEAD(&temp, stailq);
1231547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n");
1232547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
1233b846efd7SJim Harris 		_nvme_qpair_submit_request(qpair, req);
1234b846efd7SJim Harris 	}
1235b846efd7SJim Harris 
1236b846efd7SJim Harris 	mtx_unlock(&qpair->lock);
1237b846efd7SJim Harris }
1238b846efd7SJim Harris 
1239b846efd7SJim Harris static void
1240b846efd7SJim Harris nvme_qpair_disable(struct nvme_qpair *qpair)
1241b846efd7SJim Harris {
1242b846efd7SJim Harris 	struct nvme_tracker *tr;
1243b846efd7SJim Harris 
12447588c6ccSWarner Losh 	qpair->is_enabled = false;
1245b846efd7SJim Harris 	mtx_lock(&qpair->lock);
1246b846efd7SJim Harris 	TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq)
1247b846efd7SJim Harris 		callout_stop(&tr->timer);
1248b846efd7SJim Harris 	mtx_unlock(&qpair->lock);
1249b846efd7SJim Harris }
1250b846efd7SJim Harris 
1251b846efd7SJim Harris void
1252b846efd7SJim Harris nvme_admin_qpair_disable(struct nvme_qpair *qpair)
1253b846efd7SJim Harris {
1254b846efd7SJim Harris 
1255b846efd7SJim Harris 	nvme_qpair_disable(qpair);
1256b846efd7SJim Harris 	nvme_admin_qpair_abort_aers(qpair);
1257b846efd7SJim Harris }
1258b846efd7SJim Harris 
1259b846efd7SJim Harris void
1260b846efd7SJim Harris nvme_io_qpair_disable(struct nvme_qpair *qpair)
1261b846efd7SJim Harris {
1262b846efd7SJim Harris 
1263b846efd7SJim Harris 	nvme_qpair_disable(qpair);
1264b846efd7SJim Harris }
1265232e2edbSJim Harris 
1266232e2edbSJim Harris void
1267232e2edbSJim Harris nvme_qpair_fail(struct nvme_qpair *qpair)
1268232e2edbSJim Harris {
1269232e2edbSJim Harris 	struct nvme_tracker		*tr;
1270232e2edbSJim Harris 	struct nvme_request		*req;
1271232e2edbSJim Harris 
1272824073fbSWarner Losh 	if (!mtx_initialized(&qpair->lock))
1273824073fbSWarner Losh 		return;
1274824073fbSWarner Losh 
1275232e2edbSJim Harris 	mtx_lock(&qpair->lock);
1276232e2edbSJim Harris 
1277232e2edbSJim Harris 	while (!STAILQ_EMPTY(&qpair->queued_req)) {
1278232e2edbSJim Harris 		req = STAILQ_FIRST(&qpair->queued_req);
1279232e2edbSJim Harris 		STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
1280547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "failing queued i/o\n");
1281232e2edbSJim Harris 		mtx_unlock(&qpair->lock);
1282232e2edbSJim Harris 		nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC,
12832ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST);
1284232e2edbSJim Harris 		mtx_lock(&qpair->lock);
1285232e2edbSJim Harris 	}
1286232e2edbSJim Harris 
1287232e2edbSJim Harris 	/* Manually abort each outstanding I/O. */
1288232e2edbSJim Harris 	while (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
1289232e2edbSJim Harris 		tr = TAILQ_FIRST(&qpair->outstanding_tr);
1290232e2edbSJim Harris 		/*
1291232e2edbSJim Harris 		 * Do not remove the tracker.  The abort_tracker path will
1292232e2edbSJim Harris 		 *  do that for us.
1293232e2edbSJim Harris 		 */
1294547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "failing outstanding i/o\n");
1295232e2edbSJim Harris 		mtx_unlock(&qpair->lock);
129643393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
12972ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST, DO_NOT_RETRY, ERROR_PRINT_ALL);
1298232e2edbSJim Harris 		mtx_lock(&qpair->lock);
1299232e2edbSJim Harris 	}
1300232e2edbSJim Harris 
1301232e2edbSJim Harris 	mtx_unlock(&qpair->lock);
1302232e2edbSJim Harris }
1303