xref: /freebsd/sys/dev/nvme/nvme_qpair.c (revision a69c0964625f4e20a7d5f22d51e036e13eedbeeb)
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 	uint16_t	opc;
51547d523eSJim Harris 	const char *	str;
52547d523eSJim Harris };
53547d523eSJim Harris 
54547d523eSJim Harris static struct nvme_opcode_string admin_opcode[] = {
55547d523eSJim Harris 	{ NVME_OPC_DELETE_IO_SQ, "DELETE IO SQ" },
56547d523eSJim Harris 	{ NVME_OPC_CREATE_IO_SQ, "CREATE IO SQ" },
57547d523eSJim Harris 	{ NVME_OPC_GET_LOG_PAGE, "GET LOG PAGE" },
58547d523eSJim Harris 	{ NVME_OPC_DELETE_IO_CQ, "DELETE IO CQ" },
59547d523eSJim Harris 	{ NVME_OPC_CREATE_IO_CQ, "CREATE IO CQ" },
60547d523eSJim Harris 	{ NVME_OPC_IDENTIFY, "IDENTIFY" },
61547d523eSJim Harris 	{ NVME_OPC_ABORT, "ABORT" },
62547d523eSJim Harris 	{ NVME_OPC_SET_FEATURES, "SET FEATURES" },
63547d523eSJim Harris 	{ NVME_OPC_GET_FEATURES, "GET FEATURES" },
64547d523eSJim Harris 	{ NVME_OPC_ASYNC_EVENT_REQUEST, "ASYNC EVENT REQUEST" },
65547d523eSJim Harris 	{ NVME_OPC_FIRMWARE_ACTIVATE, "FIRMWARE ACTIVATE" },
66547d523eSJim Harris 	{ NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD, "FIRMWARE IMAGE DOWNLOAD" },
676b1a96b1SAlexander Motin 	{ NVME_OPC_DEVICE_SELF_TEST, "DEVICE SELF-TEST" },
686b1a96b1SAlexander Motin 	{ NVME_OPC_NAMESPACE_ATTACHMENT, "NAMESPACE ATTACHMENT" },
696b1a96b1SAlexander Motin 	{ NVME_OPC_KEEP_ALIVE, "KEEP ALIVE" },
706b1a96b1SAlexander Motin 	{ NVME_OPC_DIRECTIVE_SEND, "DIRECTIVE SEND" },
716b1a96b1SAlexander Motin 	{ NVME_OPC_DIRECTIVE_RECEIVE, "DIRECTIVE RECEIVE" },
726b1a96b1SAlexander Motin 	{ NVME_OPC_VIRTUALIZATION_MANAGEMENT, "VIRTUALIZATION MANAGEMENT" },
736b1a96b1SAlexander Motin 	{ NVME_OPC_NVME_MI_SEND, "NVME-MI SEND" },
746b1a96b1SAlexander Motin 	{ NVME_OPC_NVME_MI_RECEIVE, "NVME-MI RECEIVE" },
756b1a96b1SAlexander Motin 	{ NVME_OPC_DOORBELL_BUFFER_CONFIG, "DOORBELL BUFFER CONFIG" },
76547d523eSJim Harris 	{ NVME_OPC_FORMAT_NVM, "FORMAT NVM" },
77547d523eSJim Harris 	{ NVME_OPC_SECURITY_SEND, "SECURITY SEND" },
78547d523eSJim Harris 	{ NVME_OPC_SECURITY_RECEIVE, "SECURITY RECEIVE" },
796b1a96b1SAlexander Motin 	{ NVME_OPC_SANITIZE, "SANITIZE" },
8090dfa8f0SAlexander Motin 	{ NVME_OPC_GET_LBA_STATUS, "GET LBA STATUS" },
81547d523eSJim Harris 	{ 0xFFFF, "ADMIN COMMAND" }
82547d523eSJim Harris };
83547d523eSJim Harris 
84547d523eSJim Harris static struct nvme_opcode_string io_opcode[] = {
85547d523eSJim Harris 	{ NVME_OPC_FLUSH, "FLUSH" },
86547d523eSJim Harris 	{ NVME_OPC_WRITE, "WRITE" },
87547d523eSJim Harris 	{ NVME_OPC_READ, "READ" },
88547d523eSJim Harris 	{ NVME_OPC_WRITE_UNCORRECTABLE, "WRITE UNCORRECTABLE" },
89547d523eSJim Harris 	{ NVME_OPC_COMPARE, "COMPARE" },
906b1a96b1SAlexander Motin 	{ NVME_OPC_WRITE_ZEROES, "WRITE ZEROES" },
91547d523eSJim Harris 	{ NVME_OPC_DATASET_MANAGEMENT, "DATASET MANAGEMENT" },
9290dfa8f0SAlexander Motin 	{ NVME_OPC_VERIFY, "VERIFY" },
936b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_REGISTER, "RESERVATION REGISTER" },
946b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_REPORT, "RESERVATION REPORT" },
956b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_ACQUIRE, "RESERVATION ACQUIRE" },
966b1a96b1SAlexander Motin 	{ NVME_OPC_RESERVATION_RELEASE, "RESERVATION RELEASE" },
97547d523eSJim Harris 	{ 0xFFFF, "IO COMMAND" }
98547d523eSJim Harris };
99547d523eSJim Harris 
100547d523eSJim Harris static const char *
101547d523eSJim Harris get_admin_opcode_string(uint16_t opc)
102547d523eSJim Harris {
103547d523eSJim Harris 	struct nvme_opcode_string *entry;
104547d523eSJim Harris 
105547d523eSJim Harris 	entry = admin_opcode;
106547d523eSJim Harris 
107547d523eSJim Harris 	while (entry->opc != 0xFFFF) {
108547d523eSJim Harris 		if (entry->opc == opc)
109547d523eSJim Harris 			return (entry->str);
110547d523eSJim Harris 		entry++;
111547d523eSJim Harris 	}
112547d523eSJim Harris 	return (entry->str);
113547d523eSJim Harris }
114547d523eSJim Harris 
115547d523eSJim Harris static const char *
116547d523eSJim Harris get_io_opcode_string(uint16_t opc)
117547d523eSJim Harris {
118547d523eSJim Harris 	struct nvme_opcode_string *entry;
119547d523eSJim Harris 
120547d523eSJim Harris 	entry = io_opcode;
121547d523eSJim Harris 
122547d523eSJim Harris 	while (entry->opc != 0xFFFF) {
123547d523eSJim Harris 		if (entry->opc == opc)
124547d523eSJim Harris 			return (entry->str);
125547d523eSJim Harris 		entry++;
126547d523eSJim Harris 	}
127547d523eSJim Harris 	return (entry->str);
128547d523eSJim Harris }
129547d523eSJim Harris 
130547d523eSJim Harris static void
131547d523eSJim Harris nvme_admin_qpair_print_command(struct nvme_qpair *qpair,
132547d523eSJim Harris     struct nvme_command *cmd)
133547d523eSJim Harris {
134547d523eSJim Harris 
135547d523eSJim Harris 	nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%x "
136547d523eSJim Harris 	    "cdw10:%08x cdw11:%08x\n",
1379544e6dcSChuck Tuffli 	    get_admin_opcode_string(cmd->opc), cmd->opc, qpair->id, cmd->cid,
1380d787e9bSWojciech Macek 	    le32toh(cmd->nsid), le32toh(cmd->cdw10), le32toh(cmd->cdw11));
139547d523eSJim Harris }
140547d523eSJim Harris 
141547d523eSJim Harris static void
142547d523eSJim Harris nvme_io_qpair_print_command(struct nvme_qpair *qpair,
143547d523eSJim Harris     struct nvme_command *cmd)
144547d523eSJim Harris {
145547d523eSJim Harris 
1469544e6dcSChuck Tuffli 	switch (cmd->opc) {
147547d523eSJim Harris 	case NVME_OPC_WRITE:
148547d523eSJim Harris 	case NVME_OPC_READ:
149547d523eSJim Harris 	case NVME_OPC_WRITE_UNCORRECTABLE:
150547d523eSJim Harris 	case NVME_OPC_COMPARE:
1516b1a96b1SAlexander Motin 	case NVME_OPC_WRITE_ZEROES:
15290dfa8f0SAlexander Motin 	case NVME_OPC_VERIFY:
153547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d "
154bdd1fd40SJim Harris 		    "lba:%llu len:%d\n",
1559544e6dcSChuck Tuffli 		    get_io_opcode_string(cmd->opc), qpair->id, cmd->cid, le32toh(cmd->nsid),
1560d787e9bSWojciech Macek 		    ((unsigned long long)le32toh(cmd->cdw11) << 32) + le32toh(cmd->cdw10),
1570d787e9bSWojciech Macek 		    (le32toh(cmd->cdw12) & 0xFFFF) + 1);
158547d523eSJim Harris 		break;
159547d523eSJim Harris 	case NVME_OPC_FLUSH:
160547d523eSJim Harris 	case NVME_OPC_DATASET_MANAGEMENT:
1616b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_REGISTER:
1626b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_REPORT:
1636b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_ACQUIRE:
1646b1a96b1SAlexander Motin 	case NVME_OPC_RESERVATION_RELEASE:
165547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n",
1669544e6dcSChuck Tuffli 		    get_io_opcode_string(cmd->opc), qpair->id, cmd->cid, le32toh(cmd->nsid));
167547d523eSJim Harris 		break;
168547d523eSJim Harris 	default:
169547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%d\n",
1709544e6dcSChuck Tuffli 		    get_io_opcode_string(cmd->opc), cmd->opc, qpair->id,
1710d787e9bSWojciech Macek 		    cmd->cid, le32toh(cmd->nsid));
172547d523eSJim Harris 		break;
173547d523eSJim Harris 	}
174547d523eSJim Harris }
175547d523eSJim Harris 
176547d523eSJim Harris static void
177547d523eSJim Harris nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd)
178547d523eSJim Harris {
179547d523eSJim Harris 	if (qpair->id == 0)
180547d523eSJim Harris 		nvme_admin_qpair_print_command(qpair, cmd);
181547d523eSJim Harris 	else
182547d523eSJim Harris 		nvme_io_qpair_print_command(qpair, cmd);
183c75bdc04SWarner Losh 	if (nvme_verbose_cmd_dump) {
184c75bdc04SWarner Losh 		nvme_printf(qpair->ctrlr,
185c75bdc04SWarner Losh 		    "nsid:%#x rsvd2:%#x rsvd3:%#x mptr:%#jx prp1:%#jx prp2:%#jx\n",
186c75bdc04SWarner Losh 		    cmd->nsid, cmd->rsvd2, cmd->rsvd3, (uintmax_t)cmd->mptr,
187c75bdc04SWarner Losh 		    (uintmax_t)cmd->prp1, (uintmax_t)cmd->prp2);
188c75bdc04SWarner Losh 		nvme_printf(qpair->ctrlr,
189c75bdc04SWarner Losh 		    "cdw10: %#x cdw11:%#x cdw12:%#x cdw13:%#x cdw14:%#x cdw15:%#x\n",
190c75bdc04SWarner Losh 		    cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14,
191c75bdc04SWarner Losh 		    cmd->cdw15);
192c75bdc04SWarner Losh 	}
193547d523eSJim Harris }
194547d523eSJim Harris 
195547d523eSJim Harris struct nvme_status_string {
196547d523eSJim Harris 	uint16_t	sc;
197547d523eSJim Harris 	const char *	str;
198547d523eSJim Harris };
199547d523eSJim Harris 
200547d523eSJim Harris static struct nvme_status_string generic_status[] = {
201547d523eSJim Harris 	{ NVME_SC_SUCCESS, "SUCCESS" },
202547d523eSJim Harris 	{ NVME_SC_INVALID_OPCODE, "INVALID OPCODE" },
203547d523eSJim Harris 	{ NVME_SC_INVALID_FIELD, "INVALID_FIELD" },
204547d523eSJim Harris 	{ NVME_SC_COMMAND_ID_CONFLICT, "COMMAND ID CONFLICT" },
205547d523eSJim Harris 	{ NVME_SC_DATA_TRANSFER_ERROR, "DATA TRANSFER ERROR" },
206547d523eSJim Harris 	{ NVME_SC_ABORTED_POWER_LOSS, "ABORTED - POWER LOSS" },
207547d523eSJim Harris 	{ NVME_SC_INTERNAL_DEVICE_ERROR, "INTERNAL DEVICE ERROR" },
208547d523eSJim Harris 	{ NVME_SC_ABORTED_BY_REQUEST, "ABORTED - BY REQUEST" },
209547d523eSJim Harris 	{ NVME_SC_ABORTED_SQ_DELETION, "ABORTED - SQ DELETION" },
210547d523eSJim Harris 	{ NVME_SC_ABORTED_FAILED_FUSED, "ABORTED - FAILED FUSED" },
211547d523eSJim Harris 	{ NVME_SC_ABORTED_MISSING_FUSED, "ABORTED - MISSING FUSED" },
212547d523eSJim Harris 	{ NVME_SC_INVALID_NAMESPACE_OR_FORMAT, "INVALID NAMESPACE OR FORMAT" },
213547d523eSJim Harris 	{ NVME_SC_COMMAND_SEQUENCE_ERROR, "COMMAND SEQUENCE ERROR" },
2146b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_SGL_SEGMENT_DESCR, "INVALID SGL SEGMENT DESCRIPTOR" },
2156b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_NUMBER_OF_SGL_DESCR, "INVALID NUMBER OF SGL DESCRIPTORS" },
2166b1a96b1SAlexander Motin 	{ NVME_SC_DATA_SGL_LENGTH_INVALID, "DATA SGL LENGTH INVALID" },
2176b1a96b1SAlexander Motin 	{ NVME_SC_METADATA_SGL_LENGTH_INVALID, "METADATA SGL LENGTH INVALID" },
2186b1a96b1SAlexander Motin 	{ NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID, "SGL DESCRIPTOR TYPE INVALID" },
2196b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_USE_OF_CMB, "INVALID USE OF CONTROLLER MEMORY BUFFER" },
2206b1a96b1SAlexander Motin 	{ NVME_SC_PRP_OFFET_INVALID, "PRP OFFET INVALID" },
2216b1a96b1SAlexander Motin 	{ NVME_SC_ATOMIC_WRITE_UNIT_EXCEEDED, "ATOMIC WRITE UNIT EXCEEDED" },
2226b1a96b1SAlexander Motin 	{ NVME_SC_OPERATION_DENIED, "OPERATION DENIED" },
2236b1a96b1SAlexander Motin 	{ NVME_SC_SGL_OFFSET_INVALID, "SGL OFFSET INVALID" },
2246b1a96b1SAlexander Motin 	{ NVME_SC_HOST_ID_INCONSISTENT_FORMAT, "HOST IDENTIFIER INCONSISTENT FORMAT" },
2256b1a96b1SAlexander Motin 	{ NVME_SC_KEEP_ALIVE_TIMEOUT_EXPIRED, "KEEP ALIVE TIMEOUT EXPIRED" },
2266b1a96b1SAlexander Motin 	{ NVME_SC_KEEP_ALIVE_TIMEOUT_INVALID, "KEEP ALIVE TIMEOUT INVALID" },
2276b1a96b1SAlexander Motin 	{ NVME_SC_ABORTED_DUE_TO_PREEMPT, "COMMAND ABORTED DUE TO PREEMPT AND ABORT" },
2286b1a96b1SAlexander Motin 	{ NVME_SC_SANITIZE_FAILED, "SANITIZE FAILED" },
2296b1a96b1SAlexander Motin 	{ NVME_SC_SANITIZE_IN_PROGRESS, "SANITIZE IN PROGRESS" },
2306b1a96b1SAlexander Motin 	{ NVME_SC_SGL_DATA_BLOCK_GRAN_INVALID, "SGL_DATA_BLOCK_GRANULARITY_INVALID" },
2316b1a96b1SAlexander Motin 	{ NVME_SC_NOT_SUPPORTED_IN_CMB, "COMMAND NOT SUPPORTED FOR QUEUE IN CMB" },
23290dfa8f0SAlexander Motin 	{ NVME_SC_NAMESPACE_IS_WRITE_PROTECTED, "NAMESPACE IS WRITE PROTECTED" },
23390dfa8f0SAlexander Motin 	{ NVME_SC_COMMAND_INTERRUPTED, "COMMAND INTERRUPTED" },
23490dfa8f0SAlexander Motin 	{ NVME_SC_TRANSIENT_TRANSPORT_ERROR, "TRANSIENT TRANSPORT ERROR" },
2356b1a96b1SAlexander Motin 
236547d523eSJim Harris 	{ NVME_SC_LBA_OUT_OF_RANGE, "LBA OUT OF RANGE" },
237547d523eSJim Harris 	{ NVME_SC_CAPACITY_EXCEEDED, "CAPACITY EXCEEDED" },
238547d523eSJim Harris 	{ NVME_SC_NAMESPACE_NOT_READY, "NAMESPACE NOT READY" },
2396b1a96b1SAlexander Motin 	{ NVME_SC_RESERVATION_CONFLICT, "RESERVATION CONFLICT" },
2406b1a96b1SAlexander Motin 	{ NVME_SC_FORMAT_IN_PROGRESS, "FORMAT IN PROGRESS" },
241547d523eSJim Harris 	{ 0xFFFF, "GENERIC" }
242547d523eSJim Harris };
243547d523eSJim Harris 
244547d523eSJim Harris static struct nvme_status_string command_specific_status[] = {
245547d523eSJim Harris 	{ NVME_SC_COMPLETION_QUEUE_INVALID, "INVALID COMPLETION QUEUE" },
246547d523eSJim Harris 	{ NVME_SC_INVALID_QUEUE_IDENTIFIER, "INVALID QUEUE IDENTIFIER" },
247547d523eSJim Harris 	{ NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED, "MAX QUEUE SIZE EXCEEDED" },
248547d523eSJim Harris 	{ NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED, "ABORT CMD LIMIT EXCEEDED" },
249547d523eSJim Harris 	{ NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED, "ASYNC LIMIT EXCEEDED" },
250547d523eSJim Harris 	{ NVME_SC_INVALID_FIRMWARE_SLOT, "INVALID FIRMWARE SLOT" },
251547d523eSJim Harris 	{ NVME_SC_INVALID_FIRMWARE_IMAGE, "INVALID FIRMWARE IMAGE" },
252547d523eSJim Harris 	{ NVME_SC_INVALID_INTERRUPT_VECTOR, "INVALID INTERRUPT VECTOR" },
253547d523eSJim Harris 	{ NVME_SC_INVALID_LOG_PAGE, "INVALID LOG PAGE" },
254547d523eSJim Harris 	{ NVME_SC_INVALID_FORMAT, "INVALID FORMAT" },
255547d523eSJim Harris 	{ NVME_SC_FIRMWARE_REQUIRES_RESET, "FIRMWARE REQUIRES RESET" },
2566b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_QUEUE_DELETION, "INVALID QUEUE DELETION" },
2576b1a96b1SAlexander Motin 	{ NVME_SC_FEATURE_NOT_SAVEABLE, "FEATURE IDENTIFIER NOT SAVEABLE" },
2586b1a96b1SAlexander Motin 	{ NVME_SC_FEATURE_NOT_CHANGEABLE, "FEATURE NOT CHANGEABLE" },
2596b1a96b1SAlexander Motin 	{ NVME_SC_FEATURE_NOT_NS_SPECIFIC, "FEATURE NOT NAMESPACE SPECIFIC" },
2606b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_REQUIRES_NVMS_RESET, "FIRMWARE ACTIVATION REQUIRES NVM SUBSYSTEM RESET" },
2616b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_REQUIRES_RESET, "FIRMWARE ACTIVATION REQUIRES RESET" },
2626b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_REQUIRES_TIME, "FIRMWARE ACTIVATION REQUIRES MAXIMUM TIME VIOLATION" },
2636b1a96b1SAlexander Motin 	{ NVME_SC_FW_ACT_PROHIBITED, "FIRMWARE ACTIVATION PROHIBITED" },
2646b1a96b1SAlexander Motin 	{ NVME_SC_OVERLAPPING_RANGE, "OVERLAPPING RANGE" },
2656b1a96b1SAlexander Motin 	{ NVME_SC_NS_INSUFFICIENT_CAPACITY, "NAMESPACE INSUFFICIENT CAPACITY" },
2666b1a96b1SAlexander Motin 	{ NVME_SC_NS_ID_UNAVAILABLE, "NAMESPACE IDENTIFIER UNAVAILABLE" },
2676b1a96b1SAlexander Motin 	{ NVME_SC_NS_ALREADY_ATTACHED, "NAMESPACE ALREADY ATTACHED" },
2686b1a96b1SAlexander Motin 	{ NVME_SC_NS_IS_PRIVATE, "NAMESPACE IS PRIVATE" },
2696b1a96b1SAlexander Motin 	{ NVME_SC_NS_NOT_ATTACHED, "NS NOT ATTACHED" },
2706b1a96b1SAlexander Motin 	{ NVME_SC_THIN_PROV_NOT_SUPPORTED, "THIN PROVISIONING NOT SUPPORTED" },
2716b1a96b1SAlexander Motin 	{ NVME_SC_CTRLR_LIST_INVALID, "CONTROLLER LIST INVALID" },
2728d08cdc7SChuck Tuffli 	{ NVME_SC_SELF_TEST_IN_PROGRESS, "DEVICE SELF-TEST IN PROGRESS" },
2736b1a96b1SAlexander Motin 	{ NVME_SC_BOOT_PART_WRITE_PROHIB, "BOOT PARTITION WRITE PROHIBITED" },
2746b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_CTRLR_ID, "INVALID CONTROLLER IDENTIFIER" },
2756b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_SEC_CTRLR_STATE, "INVALID SECONDARY CONTROLLER STATE" },
2766b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_NUM_OF_CTRLR_RESRC, "INVALID NUMBER OF CONTROLLER RESOURCES" },
2776b1a96b1SAlexander Motin 	{ NVME_SC_INVALID_RESOURCE_ID, "INVALID RESOURCE IDENTIFIER" },
27890dfa8f0SAlexander Motin 	{ NVME_SC_SANITIZE_PROHIBITED_WPMRE, "SANITIZE PROHIBITED WRITE PERSISTENT MEMORY REGION ENABLED" },
27990dfa8f0SAlexander Motin 	{ NVME_SC_ANA_GROUP_ID_INVALID, "ANA GROUP IDENTIFIED INVALID" },
28090dfa8f0SAlexander Motin 	{ NVME_SC_ANA_ATTACH_FAILED, "ANA ATTACH FAILED" },
2816b1a96b1SAlexander Motin 
282547d523eSJim Harris 	{ NVME_SC_CONFLICTING_ATTRIBUTES, "CONFLICTING ATTRIBUTES" },
283547d523eSJim Harris 	{ NVME_SC_INVALID_PROTECTION_INFO, "INVALID PROTECTION INFO" },
284547d523eSJim Harris 	{ NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE, "WRITE TO RO PAGE" },
285547d523eSJim Harris 	{ 0xFFFF, "COMMAND SPECIFIC" }
286547d523eSJim Harris };
287547d523eSJim Harris 
288547d523eSJim Harris static struct nvme_status_string media_error_status[] = {
289547d523eSJim Harris 	{ NVME_SC_WRITE_FAULTS, "WRITE FAULTS" },
290547d523eSJim Harris 	{ NVME_SC_UNRECOVERED_READ_ERROR, "UNRECOVERED READ ERROR" },
291547d523eSJim Harris 	{ NVME_SC_GUARD_CHECK_ERROR, "GUARD CHECK ERROR" },
292547d523eSJim Harris 	{ NVME_SC_APPLICATION_TAG_CHECK_ERROR, "APPLICATION TAG CHECK ERROR" },
293547d523eSJim Harris 	{ NVME_SC_REFERENCE_TAG_CHECK_ERROR, "REFERENCE TAG CHECK ERROR" },
294547d523eSJim Harris 	{ NVME_SC_COMPARE_FAILURE, "COMPARE FAILURE" },
295547d523eSJim Harris 	{ NVME_SC_ACCESS_DENIED, "ACCESS DENIED" },
2966b1a96b1SAlexander Motin 	{ NVME_SC_DEALLOCATED_OR_UNWRITTEN, "DEALLOCATED OR UNWRITTEN LOGICAL BLOCK" },
297547d523eSJim Harris 	{ 0xFFFF, "MEDIA ERROR" }
298547d523eSJim Harris };
299547d523eSJim Harris 
300a6d222ebSAlexander Motin static struct nvme_status_string path_related_status[] = {
301a6d222ebSAlexander Motin 	{ NVME_SC_INTERNAL_PATH_ERROR, "INTERNAL PATH ERROR" },
302a6d222ebSAlexander Motin 	{ NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS, "ASYMMETRIC ACCESS PERSISTENT LOSS" },
303a6d222ebSAlexander Motin 	{ NVME_SC_ASYMMETRIC_ACCESS_INACCESSIBLE, "ASYMMETRIC ACCESS INACCESSIBLE" },
304a6d222ebSAlexander Motin 	{ NVME_SC_ASYMMETRIC_ACCESS_TRANSITION, "ASYMMETRIC ACCESS TRANSITION" },
305a6d222ebSAlexander Motin 	{ NVME_SC_CONTROLLER_PATHING_ERROR, "CONTROLLER PATHING ERROR" },
306a6d222ebSAlexander Motin 	{ NVME_SC_HOST_PATHING_ERROR, "HOST PATHING ERROR" },
307a6d222ebSAlexander Motin 	{ NVME_SC_COMMAND_ABOTHED_BY_HOST, "COMMAND ABOTHED BY HOST" },
308a6d222ebSAlexander Motin 	{ 0xFFFF, "PATH RELATED" },
309a6d222ebSAlexander Motin };
310a6d222ebSAlexander Motin 
311547d523eSJim Harris static const char *
312547d523eSJim Harris get_status_string(uint16_t sct, uint16_t sc)
313547d523eSJim Harris {
314547d523eSJim Harris 	struct nvme_status_string *entry;
315547d523eSJim Harris 
316547d523eSJim Harris 	switch (sct) {
317547d523eSJim Harris 	case NVME_SCT_GENERIC:
318547d523eSJim Harris 		entry = generic_status;
319547d523eSJim Harris 		break;
320547d523eSJim Harris 	case NVME_SCT_COMMAND_SPECIFIC:
321547d523eSJim Harris 		entry = command_specific_status;
322547d523eSJim Harris 		break;
323547d523eSJim Harris 	case NVME_SCT_MEDIA_ERROR:
324547d523eSJim Harris 		entry = media_error_status;
325547d523eSJim Harris 		break;
326a6d222ebSAlexander Motin 	case NVME_SCT_PATH_RELATED:
327a6d222ebSAlexander Motin 		entry = path_related_status;
328a6d222ebSAlexander Motin 		break;
329547d523eSJim Harris 	case NVME_SCT_VENDOR_SPECIFIC:
330547d523eSJim Harris 		return ("VENDOR SPECIFIC");
331547d523eSJim Harris 	default:
332547d523eSJim Harris 		return ("RESERVED");
333547d523eSJim Harris 	}
334547d523eSJim Harris 
335547d523eSJim Harris 	while (entry->sc != 0xFFFF) {
336547d523eSJim Harris 		if (entry->sc == sc)
337547d523eSJim Harris 			return (entry->str);
338547d523eSJim Harris 		entry++;
339547d523eSJim Harris 	}
340547d523eSJim Harris 	return (entry->str);
341547d523eSJim Harris }
342547d523eSJim Harris 
343547d523eSJim Harris static void
344547d523eSJim Harris nvme_qpair_print_completion(struct nvme_qpair *qpair,
345547d523eSJim Harris     struct nvme_completion *cpl)
346547d523eSJim Harris {
347*a69c0964SAlexander Motin 	uint8_t sct, sc, crd, m, dnr;
3480d787e9bSWojciech Macek 
3490d787e9bSWojciech Macek 	sct = NVME_STATUS_GET_SCT(cpl->status);
3500d787e9bSWojciech Macek 	sc = NVME_STATUS_GET_SC(cpl->status);
351*a69c0964SAlexander Motin 	crd = NVME_STATUS_GET_CRD(cpl->status);
352*a69c0964SAlexander Motin 	m = NVME_STATUS_GET_M(cpl->status);
353*a69c0964SAlexander Motin 	dnr = NVME_STATUS_GET_DNR(cpl->status);
3540d787e9bSWojciech Macek 
355*a69c0964SAlexander Motin 	nvme_printf(qpair->ctrlr, "%s (%02x/%02x) crd:%x m:%x dnr:%x "
356*a69c0964SAlexander Motin 	    "sqid:%d cid:%d cdw0:%x\n",
357*a69c0964SAlexander Motin 	    get_status_string(sct, sc), sct, sc, crd, m, dnr,
358*a69c0964SAlexander Motin 	    cpl->sqid, cpl->cid, cpl->cdw0);
359547d523eSJim Harris }
360547d523eSJim Harris 
3617588c6ccSWarner Losh static bool
3626cb06070SJim Harris nvme_completion_is_retry(const struct nvme_completion *cpl)
363bb0ec6b3SJim Harris {
3640d787e9bSWojciech Macek 	uint8_t sct, sc, dnr;
3650d787e9bSWojciech Macek 
3660d787e9bSWojciech Macek 	sct = NVME_STATUS_GET_SCT(cpl->status);
3670d787e9bSWojciech Macek 	sc = NVME_STATUS_GET_SC(cpl->status);
3682ffd6fceSWarner Losh 	dnr = NVME_STATUS_GET_DNR(cpl->status);	/* Do Not Retry Bit */
3690d787e9bSWojciech Macek 
370bb0ec6b3SJim Harris 	/*
371bb0ec6b3SJim Harris 	 * TODO: spec is not clear how commands that are aborted due
372bb0ec6b3SJim Harris 	 *  to TLER will be marked.  So for now, it seems
373bb0ec6b3SJim Harris 	 *  NAMESPACE_NOT_READY is the only case where we should
37495108cadSWarner Losh 	 *  look at the DNR bit. Requests failed with ABORTED_BY_REQUEST
37595108cadSWarner Losh 	 *  set the DNR bit correctly since the driver controls that.
376bb0ec6b3SJim Harris 	 */
3770d787e9bSWojciech Macek 	switch (sct) {
378bb0ec6b3SJim Harris 	case NVME_SCT_GENERIC:
3790d787e9bSWojciech Macek 		switch (sc) {
380448195e7SJim Harris 		case NVME_SC_ABORTED_BY_REQUEST:
381bb0ec6b3SJim Harris 		case NVME_SC_NAMESPACE_NOT_READY:
3820d787e9bSWojciech Macek 			if (dnr)
383bb0ec6b3SJim Harris 				return (0);
384bb0ec6b3SJim Harris 			else
385bb0ec6b3SJim Harris 				return (1);
386bb0ec6b3SJim Harris 		case NVME_SC_INVALID_OPCODE:
387bb0ec6b3SJim Harris 		case NVME_SC_INVALID_FIELD:
388bb0ec6b3SJim Harris 		case NVME_SC_COMMAND_ID_CONFLICT:
389bb0ec6b3SJim Harris 		case NVME_SC_DATA_TRANSFER_ERROR:
390bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_POWER_LOSS:
391bb0ec6b3SJim Harris 		case NVME_SC_INTERNAL_DEVICE_ERROR:
392bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_SQ_DELETION:
393bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_FAILED_FUSED:
394bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_MISSING_FUSED:
395bb0ec6b3SJim Harris 		case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
396bb0ec6b3SJim Harris 		case NVME_SC_COMMAND_SEQUENCE_ERROR:
397bb0ec6b3SJim Harris 		case NVME_SC_LBA_OUT_OF_RANGE:
398bb0ec6b3SJim Harris 		case NVME_SC_CAPACITY_EXCEEDED:
399bb0ec6b3SJim Harris 		default:
400bb0ec6b3SJim Harris 			return (0);
401bb0ec6b3SJim Harris 		}
402bb0ec6b3SJim Harris 	case NVME_SCT_COMMAND_SPECIFIC:
403bb0ec6b3SJim Harris 	case NVME_SCT_MEDIA_ERROR:
404a6d222ebSAlexander Motin 		return (0);
405a6d222ebSAlexander Motin 	case NVME_SCT_PATH_RELATED:
406a6d222ebSAlexander Motin 		switch (sc) {
407a6d222ebSAlexander Motin 		case NVME_SC_INTERNAL_PATH_ERROR:
408a6d222ebSAlexander Motin 			if (dnr)
409a6d222ebSAlexander Motin 				return (0);
410a6d222ebSAlexander Motin 			else
411a6d222ebSAlexander Motin 				return (1);
412a6d222ebSAlexander Motin 		default:
413a6d222ebSAlexander Motin 			return (0);
414a6d222ebSAlexander Motin 		}
415bb0ec6b3SJim Harris 	case NVME_SCT_VENDOR_SPECIFIC:
416bb0ec6b3SJim Harris 	default:
417bb0ec6b3SJim Harris 		return (0);
418bb0ec6b3SJim Harris 	}
419bb0ec6b3SJim Harris }
420bb0ec6b3SJim Harris 
42121b6da58SJim Harris static void
42243393e8bSWarner Losh nvme_qpair_complete_tracker(struct nvme_tracker *tr,
4232ffd6fceSWarner Losh     struct nvme_completion *cpl, error_print_t print_on_error)
424bb0ec6b3SJim Harris {
42543393e8bSWarner Losh 	struct nvme_qpair * qpair = tr->qpair;
426ad697276SJim Harris 	struct nvme_request	*req;
4277588c6ccSWarner Losh 	bool			retry, error, retriable;
428bb0ec6b3SJim Harris 
429ad697276SJim Harris 	req = tr->req;
4306cb06070SJim Harris 	error = nvme_completion_is_error(cpl);
4315e83c2ffSWarner Losh 	retriable = nvme_completion_is_retry(cpl);
4325e83c2ffSWarner Losh 	retry = error && retriable && req->retries < nvme_retry_count;
433c37fc318SWarner Losh 	if (retry)
434c37fc318SWarner Losh 		qpair->num_retries++;
4355e83c2ffSWarner Losh 	if (error && req->retries >= nvme_retry_count && retriable)
4365e83c2ffSWarner Losh 		qpair->num_failures++;
437ad697276SJim Harris 
4382ffd6fceSWarner Losh 	if (error && (print_on_error == ERROR_PRINT_ALL ||
4392ffd6fceSWarner Losh 		(!retry && print_on_error == ERROR_PRINT_NO_RETRY))) {
440547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
441547d523eSJim Harris 		nvme_qpair_print_completion(qpair, cpl);
442bb0ec6b3SJim Harris 	}
443bb0ec6b3SJim Harris 
444bb0ec6b3SJim Harris 	qpair->act_tr[cpl->cid] = NULL;
445bb0ec6b3SJim Harris 
4466cb06070SJim Harris 	KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n"));
447bb0ec6b3SJim Harris 
4480a4b14e8SMichal Meloun 	if (!retry) {
4490a4b14e8SMichal Meloun 		if (req->type != NVME_REQUEST_NULL) {
4500a4b14e8SMichal Meloun 			bus_dmamap_sync(qpair->dma_tag_payload,
4510a4b14e8SMichal Meloun 			    tr->payload_dma_map,
4520a4b14e8SMichal Meloun 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4530a4b14e8SMichal Meloun 		}
4540a4b14e8SMichal Meloun 		if (req->cb_fn)
455ad697276SJim Harris 			req->cb_fn(req->cb_arg, cpl);
4560a4b14e8SMichal Meloun 	}
457bb0ec6b3SJim Harris 
458bb0ec6b3SJim Harris 	mtx_lock(&qpair->lock);
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 
543b846efd7SJim Harris 	/*
544dfa01f4fSGordon Bergling 	 * qpair is not enabled, likely because a controller reset is in
545d0aaeffdSWarner Losh 	 * progress.  Ignore the interrupt - any I/O that was associated with
5467d5eebe0SWarner Losh 	 * this interrupt will get retried when the reset is complete. Any
5477d5eebe0SWarner Losh 	 * pending completions for when we're in startup will be completed
5487d5eebe0SWarner Losh 	 * as soon as initialization is complete and we start sending commands
5497d5eebe0SWarner Losh 	 * to the device.
550b846efd7SJim Harris 	 */
551587aa255SWarner Losh 	if (qpair->recovery_state != RECOVERY_NONE) {
552587aa255SWarner Losh 		qpair->num_ignored++;
553d85d9648SWarner Losh 		return (false);
554587aa255SWarner Losh 	}
555b846efd7SJim Harris 
5567d5eebe0SWarner Losh 	/*
5577d5eebe0SWarner Losh 	 * Sanity check initialization. After we reset the hardware, the phase
5587d5eebe0SWarner Losh 	 * is defined to be 1. So if we get here with zero prior calls and the
5597d5eebe0SWarner Losh 	 * phase is 0, it means that we've lost a race between the
5607d5eebe0SWarner Losh 	 * initialization and the ISR running. With the phase wrong, we'll
5617d5eebe0SWarner Losh 	 * process a bunch of completions that aren't really completions leading
5627d5eebe0SWarner Losh 	 * to a KASSERT below.
5637d5eebe0SWarner Losh 	 */
5647d5eebe0SWarner Losh 	KASSERT(!(qpair->num_intr_handler_calls == 0 && qpair->phase == 0),
5657d5eebe0SWarner Losh 	    ("%s: Phase wrong for first interrupt call.",
5667d5eebe0SWarner Losh 		device_get_nameunit(qpair->ctrlr->dev)));
5677d5eebe0SWarner Losh 
5687d5eebe0SWarner Losh 	qpair->num_intr_handler_calls++;
5697d5eebe0SWarner Losh 
5708f9d5a8dSMichal Meloun 	bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
5718f9d5a8dSMichal Meloun 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
572d0aaeffdSWarner Losh 	/*
573d0aaeffdSWarner Losh 	 * A panic can stop the CPU this routine is running on at any point.  If
574d0aaeffdSWarner Losh 	 * we're called during a panic, complete the sq_head wrap protocol for
575d0aaeffdSWarner Losh 	 * the case where we are interrupted just after the increment at 1
576d0aaeffdSWarner Losh 	 * below, but before we can reset cq_head to zero at 2. Also cope with
577d0aaeffdSWarner Losh 	 * the case where we do the zero at 2, but may or may not have done the
578d0aaeffdSWarner Losh 	 * phase adjustment at step 3. The panic machinery flushes all pending
579d0aaeffdSWarner Losh 	 * memory writes, so we can make these strong ordering assumptions
580d0aaeffdSWarner Losh 	 * that would otherwise be unwise if we were racing in real time.
581d0aaeffdSWarner Losh 	 */
582d0aaeffdSWarner Losh 	if (__predict_false(in_panic)) {
583d0aaeffdSWarner Losh 		if (qpair->cq_head == qpair->num_entries) {
584d0aaeffdSWarner Losh 			/*
585d0aaeffdSWarner Losh 			 * Here we know that we need to zero cq_head and then negate
586d0aaeffdSWarner Losh 			 * the phase, which hasn't been assigned if cq_head isn't
587d0aaeffdSWarner Losh 			 * zero due to the atomic_store_rel.
588d0aaeffdSWarner Losh 			 */
589d0aaeffdSWarner Losh 			qpair->cq_head = 0;
590d0aaeffdSWarner Losh 			qpair->phase = !qpair->phase;
591d0aaeffdSWarner Losh 		} else if (qpair->cq_head == 0) {
592d0aaeffdSWarner Losh 			/*
593d0aaeffdSWarner Losh 			 * In this case, we know that the assignment at 2
594d0aaeffdSWarner Losh 			 * happened below, but we don't know if it 3 happened or
595d0aaeffdSWarner Losh 			 * not. To do this, we look at the last completion
596d0aaeffdSWarner Losh 			 * entry and set the phase to the opposite phase
597d0aaeffdSWarner Losh 			 * that it has. This gets us back in sync
598d0aaeffdSWarner Losh 			 */
599d0aaeffdSWarner Losh 			cpl = qpair->cpl[qpair->num_entries - 1];
600d0aaeffdSWarner Losh 			nvme_completion_swapbytes(&cpl);
601d0aaeffdSWarner Losh 			qpair->phase = !NVME_STATUS_GET_P(cpl.status);
602d0aaeffdSWarner Losh 		}
603d0aaeffdSWarner Losh 	}
604d0aaeffdSWarner Losh 
6056cb06070SJim Harris 	while (1) {
606aa0ab681SWarner Losh 		uint16_t status;
6076cb06070SJim Harris 
608aa0ab681SWarner Losh 		/*
609aa0ab681SWarner Losh 		 * We need to do this dance to avoid a race between the host and
610aa0ab681SWarner Losh 		 * the device where the device overtakes the host while the host
611aa0ab681SWarner Losh 		 * is reading this record, leaving the status field 'new' and
612aa0ab681SWarner Losh 		 * the sqhd and cid fields potentially stale. If the phase
613aa0ab681SWarner Losh 		 * doesn't match, that means status hasn't yet been updated and
614aa0ab681SWarner Losh 		 * we'll get any pending changes next time. It also means that
615aa0ab681SWarner Losh 		 * the phase must be the same the second time. We have to sync
616aa0ab681SWarner Losh 		 * before reading to ensure any bouncing completes.
617aa0ab681SWarner Losh 		 */
618aa0ab681SWarner Losh 		status = le16toh(qpair->cpl[qpair->cq_head].status);
619aa0ab681SWarner Losh 		if (NVME_STATUS_GET_P(status) != qpair->phase)
620aa0ab681SWarner Losh 			break;
621aa0ab681SWarner Losh 
622aa0ab681SWarner Losh 		bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
623aa0ab681SWarner Losh 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
624aa0ab681SWarner Losh 		cpl = qpair->cpl[qpair->cq_head];
6250d787e9bSWojciech Macek 		nvme_completion_swapbytes(&cpl);
6260d787e9bSWojciech Macek 
627aa0ab681SWarner Losh 		KASSERT(
628aa0ab681SWarner Losh 		    NVME_STATUS_GET_P(status) == NVME_STATUS_GET_P(cpl.status),
629aa0ab681SWarner Losh 		    ("Phase unexpectedly inconsistent"));
6306cb06070SJim Harris 
63136a87d0cSWarner Losh 		if (cpl.cid < qpair->num_trackers)
6320d787e9bSWojciech Macek 			tr = qpair->act_tr[cpl.cid];
63336a87d0cSWarner Losh 		else
63436a87d0cSWarner Losh 			tr = NULL;
6356cb06070SJim Harris 
6362ec165e3SWarner Losh 		done++;
6376cb06070SJim Harris 		if (tr != NULL) {
63843393e8bSWarner Losh 			nvme_qpair_complete_tracker(tr, &cpl, ERROR_PRINT_ALL);
6390d787e9bSWojciech Macek 			qpair->sq_head = cpl.sqhd;
640d0aaeffdSWarner Losh 		} else if (!in_panic) {
641d0aaeffdSWarner Losh 			/*
642d0aaeffdSWarner Losh 			 * A missing tracker is normally an error.  However, a
643d0aaeffdSWarner Losh 			 * panic can stop the CPU this routine is running on
644d0aaeffdSWarner Losh 			 * after completing an I/O but before updating
645d0aaeffdSWarner Losh 			 * qpair->cq_head at 1 below.  Later, we re-enter this
646d0aaeffdSWarner Losh 			 * routine to poll I/O associated with the kernel
647d0aaeffdSWarner Losh 			 * dump. We find that the tr has been set to null before
648d0aaeffdSWarner Losh 			 * calling the completion routine.  If it hasn't
649d0aaeffdSWarner Losh 			 * completed (or it triggers a panic), then '1' below
650d0aaeffdSWarner Losh 			 * won't have updated cq_head. Rather than panic again,
651d0aaeffdSWarner Losh 			 * ignore this condition because it's not unexpected.
652d0aaeffdSWarner Losh 			 */
653547d523eSJim Harris 			nvme_printf(qpair->ctrlr,
65436a87d0cSWarner Losh 			    "cpl (cid = %u) does not map to outstanding cmd\n",
65536a87d0cSWarner Losh 				cpl.cid);
6560d787e9bSWojciech Macek 			/* nvme_dump_completion expects device endianess */
6570d787e9bSWojciech Macek 			nvme_dump_completion(&qpair->cpl[qpair->cq_head]);
658d0aaeffdSWarner Losh 			KASSERT(0, ("received completion for unknown cmd"));
6596cb06070SJim Harris 		}
660bb0ec6b3SJim Harris 
661d0aaeffdSWarner Losh 		/*
662d0aaeffdSWarner Losh 		 * There's a number of races with the following (see above) when
663d0aaeffdSWarner Losh 		 * the system panics. We compensate for each one of them by
664d0aaeffdSWarner Losh 		 * using the atomic store to force strong ordering (at least when
665d0aaeffdSWarner Losh 		 * viewed in the aftermath of a panic).
666d0aaeffdSWarner Losh 		 */
667d0aaeffdSWarner Losh 		if (++qpair->cq_head == qpair->num_entries) {		/* 1 */
668d0aaeffdSWarner Losh 			atomic_store_rel_int(&qpair->cq_head, 0);	/* 2 */
669d0aaeffdSWarner Losh 			qpair->phase = !qpair->phase;			/* 3 */
670bb0ec6b3SJim Harris 		}
6712ec165e3SWarner Losh 	}
672bb0ec6b3SJim Harris 
6732ec165e3SWarner Losh 	if (done != 0) {
674f93b7f95SWarner Losh 		bus_space_write_4(qpair->ctrlr->bus_tag, qpair->ctrlr->bus_handle,
675f93b7f95SWarner Losh 		    qpair->cq_hdbl_off, qpair->cq_head);
676bb0ec6b3SJim Harris 	}
6772ec165e3SWarner Losh 
678d85d9648SWarner Losh 	return (done != 0);
679bb0ec6b3SJim Harris }
680bb0ec6b3SJim Harris 
681bb0ec6b3SJim Harris static void
682e3bdf3daSAlexander Motin nvme_qpair_msi_handler(void *arg)
683bb0ec6b3SJim Harris {
684bb0ec6b3SJim Harris 	struct nvme_qpair *qpair = arg;
685bb0ec6b3SJim Harris 
686bb0ec6b3SJim Harris 	nvme_qpair_process_completions(qpair);
687bb0ec6b3SJim Harris }
688bb0ec6b3SJim Harris 
689a965389bSScott Long int
6901eab19cbSAlexander Motin nvme_qpair_construct(struct nvme_qpair *qpair,
6911eab19cbSAlexander Motin     uint32_t num_entries, uint32_t num_trackers,
6928d09e3c4SJim Harris     struct nvme_controller *ctrlr)
693bb0ec6b3SJim Harris {
69421b6da58SJim Harris 	struct nvme_tracker	*tr;
695a965389bSScott Long 	size_t			cmdsz, cplsz, prpsz, allocsz, prpmemsz;
696a965389bSScott Long 	uint64_t		queuemem_phys, prpmem_phys, list_phys;
697a965389bSScott Long 	uint8_t			*queuemem, *prpmem, *prp_list;
698a965389bSScott Long 	int			i, err;
699bb0ec6b3SJim Harris 
700e3bdf3daSAlexander Motin 	qpair->vector = ctrlr->msi_count > 1 ? qpair->id : 0;
701bb0ec6b3SJim Harris 	qpair->num_entries = num_entries;
7020f71ecf7SJim Harris 	qpair->num_trackers = num_trackers;
703bb0ec6b3SJim Harris 	qpair->ctrlr = ctrlr;
704bb0ec6b3SJim Harris 
705bb0ec6b3SJim Harris 	mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF);
706bb0ec6b3SJim Harris 
7071416ef36SJim Harris 	/* Note: NVMe PRP format is restricted to 4-byte alignment. */
708a6e30963SJim Harris 	err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
7090fd4cd40SWarner Losh 	    4, ctrlr->page_size, BUS_SPACE_MAXADDR,
710ac90f70dSAlexander Motin 	    BUS_SPACE_MAXADDR, NULL, NULL, ctrlr->max_xfer_size,
7110fd4cd40SWarner Losh 	    howmany(ctrlr->max_xfer_size, ctrlr->page_size) + 1,
7120fd4cd40SWarner Losh 	    ctrlr->page_size, 0,
713a6e30963SJim Harris 	    NULL, NULL, &qpair->dma_tag_payload);
714a965389bSScott Long 	if (err != 0) {
715a6e30963SJim Harris 		nvme_printf(ctrlr, "payload tag create failed %d\n", err);
716a965389bSScott Long 		goto out;
717a965389bSScott Long 	}
718a965389bSScott Long 
719a965389bSScott Long 	/*
720a965389bSScott Long 	 * Each component must be page aligned, and individual PRP lists
721a965389bSScott Long 	 * cannot cross a page boundary.
722a965389bSScott Long 	 */
723a965389bSScott Long 	cmdsz = qpair->num_entries * sizeof(struct nvme_command);
7240fd4cd40SWarner Losh 	cmdsz = roundup2(cmdsz, ctrlr->page_size);
725a965389bSScott Long 	cplsz = qpair->num_entries * sizeof(struct nvme_completion);
7260fd4cd40SWarner Losh 	cplsz = roundup2(cplsz, ctrlr->page_size);
727ac90f70dSAlexander Motin 	/*
728ac90f70dSAlexander Motin 	 * For commands requiring more than 2 PRP entries, one PRP will be
729ac90f70dSAlexander Motin 	 * embedded in the command (prp1), and the rest of the PRP entries
730ac90f70dSAlexander Motin 	 * will be in a list pointed to by the command (prp2).
731ac90f70dSAlexander Motin 	 */
7320fd4cd40SWarner Losh 	prpsz = sizeof(uint64_t) *
7330fd4cd40SWarner Losh 	    howmany(ctrlr->max_xfer_size, ctrlr->page_size);
734a965389bSScott Long 	prpmemsz = qpair->num_trackers * prpsz;
735a965389bSScott Long 	allocsz = cmdsz + cplsz + prpmemsz;
736a6e30963SJim Harris 
737a6e30963SJim Harris 	err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
7380fd4cd40SWarner Losh 	    ctrlr->page_size, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
739a965389bSScott Long 	    allocsz, 1, allocsz, 0, NULL, NULL, &qpair->dma_tag);
740a965389bSScott Long 	if (err != 0) {
741a6e30963SJim Harris 		nvme_printf(ctrlr, "tag create failed %d\n", err);
742a965389bSScott Long 		goto out;
743a965389bSScott Long 	}
7441eab19cbSAlexander Motin 	bus_dma_tag_set_domain(qpair->dma_tag, qpair->domain);
745a965389bSScott Long 
746a965389bSScott Long 	if (bus_dmamem_alloc(qpair->dma_tag, (void **)&queuemem,
7478f9d5a8dSMichal Meloun 	     BUS_DMA_COHERENT | BUS_DMA_NOWAIT, &qpair->queuemem_map)) {
748a965389bSScott Long 		nvme_printf(ctrlr, "failed to alloc qpair memory\n");
749a965389bSScott Long 		goto out;
750a965389bSScott Long 	}
751a965389bSScott Long 
752a965389bSScott Long 	if (bus_dmamap_load(qpair->dma_tag, qpair->queuemem_map,
753a965389bSScott Long 	    queuemem, allocsz, nvme_single_map, &queuemem_phys, 0) != 0) {
754a965389bSScott Long 		nvme_printf(ctrlr, "failed to load qpair memory\n");
755550d5d64SAlexander Motin 		bus_dmamem_free(qpair->dma_tag, qpair->cmd,
756550d5d64SAlexander Motin 		    qpair->queuemem_map);
757a965389bSScott Long 		goto out;
758a965389bSScott Long 	}
759bb0ec6b3SJim Harris 
760bb0ec6b3SJim Harris 	qpair->num_cmds = 0;
7616568ebfcSJim Harris 	qpair->num_intr_handler_calls = 0;
762c37fc318SWarner Losh 	qpair->num_retries = 0;
7635e83c2ffSWarner Losh 	qpair->num_failures = 0;
764587aa255SWarner Losh 	qpair->num_ignored = 0;
765a965389bSScott Long 	qpair->cmd = (struct nvme_command *)queuemem;
766a965389bSScott Long 	qpair->cpl = (struct nvme_completion *)(queuemem + cmdsz);
767a965389bSScott Long 	prpmem = (uint8_t *)(queuemem + cmdsz + cplsz);
768a965389bSScott Long 	qpair->cmd_bus_addr = queuemem_phys;
769a965389bSScott Long 	qpair->cpl_bus_addr = queuemem_phys + cmdsz;
770a965389bSScott Long 	prpmem_phys = queuemem_phys + cmdsz + cplsz;
771bb0ec6b3SJim Harris 
772502dc84aSWarner Losh 	callout_init(&qpair->timer, 1);
773502dc84aSWarner Losh 	qpair->timer_armed = false;
774fa81f373SWarner Losh 	qpair->recovery_state = RECOVERY_WAITING;
775502dc84aSWarner Losh 
776f93b7f95SWarner Losh 	/*
777f93b7f95SWarner Losh 	 * Calcuate the stride of the doorbell register. Many emulators set this
778f93b7f95SWarner Losh 	 * value to correspond to a cache line. However, some hardware has set
779f93b7f95SWarner Losh 	 * it to various small values.
780f93b7f95SWarner Losh 	 */
781f93b7f95SWarner Losh 	qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[0]) +
7821eab19cbSAlexander Motin 	    (qpair->id << (ctrlr->dstrd + 1));
783f93b7f95SWarner Losh 	qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[0]) +
7841eab19cbSAlexander Motin 	    (qpair->id << (ctrlr->dstrd + 1)) + (1 << ctrlr->dstrd);
785bb0ec6b3SJim Harris 
78665c2474eSJim Harris 	TAILQ_INIT(&qpair->free_tr);
78765c2474eSJim Harris 	TAILQ_INIT(&qpair->outstanding_tr);
7880f71ecf7SJim Harris 	STAILQ_INIT(&qpair->queued_req);
789bb0ec6b3SJim Harris 
790a965389bSScott Long 	list_phys = prpmem_phys;
791a965389bSScott Long 	prp_list = prpmem;
7920f71ecf7SJim Harris 	for (i = 0; i < qpair->num_trackers; i++) {
793a965389bSScott Long 		if (list_phys + prpsz > prpmem_phys + prpmemsz) {
794a965389bSScott Long 			qpair->num_trackers = i;
795a965389bSScott Long 			break;
79621b6da58SJim Harris 		}
79721b6da58SJim Harris 
798a965389bSScott Long 		/*
799a965389bSScott Long 		 * Make sure that the PRP list for this tracker doesn't
8000fd4cd40SWarner Losh 		 * overflow to another nvme page.
801a965389bSScott Long 		 */
802a965389bSScott Long 		if (trunc_page(list_phys) !=
803a965389bSScott Long 		    trunc_page(list_phys + prpsz - 1)) {
8040fd4cd40SWarner Losh 			list_phys = roundup2(list_phys, ctrlr->page_size);
805a965389bSScott Long 			prp_list =
8060fd4cd40SWarner Losh 			    (uint8_t *)roundup2((uintptr_t)prp_list, ctrlr->page_size);
807a965389bSScott Long 		}
808a965389bSScott Long 
8091eab19cbSAlexander Motin 		tr = malloc_domainset(sizeof(*tr), M_NVME,
8101eab19cbSAlexander Motin 		    DOMAINSET_PREF(qpair->domain), M_ZERO | M_WAITOK);
811a965389bSScott Long 		bus_dmamap_create(qpair->dma_tag_payload, 0,
812a965389bSScott Long 		    &tr->payload_dma_map);
813a965389bSScott Long 		tr->cid = i;
814a965389bSScott Long 		tr->qpair = qpair;
815a965389bSScott Long 		tr->prp = (uint64_t *)prp_list;
816a965389bSScott Long 		tr->prp_bus_addr = list_phys;
817a965389bSScott Long 		TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
818a965389bSScott Long 		list_phys += prpsz;
819a965389bSScott Long 		prp_list += prpsz;
820a965389bSScott Long 	}
821a965389bSScott Long 
822a965389bSScott Long 	if (qpair->num_trackers == 0) {
823a965389bSScott Long 		nvme_printf(ctrlr, "failed to allocate enough trackers\n");
824a965389bSScott Long 		goto out;
825a965389bSScott Long 	}
826a965389bSScott Long 
8271eab19cbSAlexander Motin 	qpair->act_tr = malloc_domainset(sizeof(struct nvme_tracker *) *
8281eab19cbSAlexander Motin 	    qpair->num_entries, M_NVME, DOMAINSET_PREF(qpair->domain),
8291eab19cbSAlexander Motin 	    M_ZERO | M_WAITOK);
830fc9a0840SWarner Losh 
831e3bdf3daSAlexander Motin 	if (ctrlr->msi_count > 1) {
832fc9a0840SWarner Losh 		/*
833fc9a0840SWarner Losh 		 * MSI-X vector resource IDs start at 1, so we add one to
834fc9a0840SWarner Losh 		 *  the queue's vector to get the corresponding rid to use.
835fc9a0840SWarner Losh 		 */
836fc9a0840SWarner Losh 		qpair->rid = qpair->vector + 1;
837fc9a0840SWarner Losh 
838fc9a0840SWarner Losh 		qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
839fc9a0840SWarner Losh 		    &qpair->rid, RF_ACTIVE);
840e3bdf3daSAlexander Motin 		if (qpair->res == NULL) {
841e3bdf3daSAlexander Motin 			nvme_printf(ctrlr, "unable to allocate MSI\n");
842e3bdf3daSAlexander Motin 			goto out;
843e3bdf3daSAlexander Motin 		}
844fc9a0840SWarner Losh 		if (bus_setup_intr(ctrlr->dev, qpair->res,
845fc9a0840SWarner Losh 		    INTR_TYPE_MISC | INTR_MPSAFE, NULL,
846e3bdf3daSAlexander Motin 		    nvme_qpair_msi_handler, qpair, &qpair->tag) != 0) {
847e3bdf3daSAlexander Motin 			nvme_printf(ctrlr, "unable to setup MSI\n");
848fc9a0840SWarner Losh 			goto out;
849fc9a0840SWarner Losh 		}
850fc9a0840SWarner Losh 		if (qpair->id == 0) {
851fc9a0840SWarner Losh 			bus_describe_intr(ctrlr->dev, qpair->res, qpair->tag,
852fc9a0840SWarner Losh 			    "admin");
853fc9a0840SWarner Losh 		} else {
854fc9a0840SWarner Losh 			bus_describe_intr(ctrlr->dev, qpair->res, qpair->tag,
855fc9a0840SWarner Losh 			    "io%d", qpair->id - 1);
856fc9a0840SWarner Losh 		}
857fc9a0840SWarner Losh 	}
858fc9a0840SWarner Losh 
859a965389bSScott Long 	return (0);
860a965389bSScott Long 
861a965389bSScott Long out:
862a965389bSScott Long 	nvme_qpair_destroy(qpair);
863a965389bSScott Long 	return (ENOMEM);
864bb0ec6b3SJim Harris }
865bb0ec6b3SJim Harris 
866bb0ec6b3SJim Harris static void
867bb0ec6b3SJim Harris nvme_qpair_destroy(struct nvme_qpair *qpair)
868bb0ec6b3SJim Harris {
869bb0ec6b3SJim Harris 	struct nvme_tracker	*tr;
870bb0ec6b3SJim Harris 
871502dc84aSWarner Losh 	callout_drain(&qpair->timer);
872502dc84aSWarner Losh 
873550d5d64SAlexander Motin 	if (qpair->tag) {
874bb0ec6b3SJim Harris 		bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag);
875550d5d64SAlexander Motin 		qpair->tag = NULL;
8763d7eb41cSJim Harris 	}
8773d7eb41cSJim Harris 
878550d5d64SAlexander Motin 	if (qpair->act_tr) {
87996ad26eeSMark Johnston 		free(qpair->act_tr, M_NVME);
880550d5d64SAlexander Motin 		qpair->act_tr = NULL;
881550d5d64SAlexander Motin 	}
882bb0ec6b3SJim Harris 
88365c2474eSJim Harris 	while (!TAILQ_EMPTY(&qpair->free_tr)) {
88465c2474eSJim Harris 		tr = TAILQ_FIRST(&qpair->free_tr);
88565c2474eSJim Harris 		TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
886c6c70c07SAlexander Motin 		bus_dmamap_destroy(qpair->dma_tag_payload,
887c6c70c07SAlexander Motin 		    tr->payload_dma_map);
88896ad26eeSMark Johnston 		free(tr, M_NVME);
889bb0ec6b3SJim Harris 	}
890c6c70c07SAlexander Motin 
891550d5d64SAlexander Motin 	if (qpair->cmd != NULL) {
892550d5d64SAlexander Motin 		bus_dmamap_unload(qpair->dma_tag, qpair->queuemem_map);
893550d5d64SAlexander Motin 		bus_dmamem_free(qpair->dma_tag, qpair->cmd,
894550d5d64SAlexander Motin 		    qpair->queuemem_map);
895550d5d64SAlexander Motin 		qpair->cmd = NULL;
896550d5d64SAlexander Motin 	}
897c6c70c07SAlexander Motin 
898550d5d64SAlexander Motin 	if (qpair->dma_tag) {
899550d5d64SAlexander Motin 		bus_dma_tag_destroy(qpair->dma_tag);
900550d5d64SAlexander Motin 		qpair->dma_tag = NULL;
901550d5d64SAlexander Motin 	}
902550d5d64SAlexander Motin 
903550d5d64SAlexander Motin 	if (qpair->dma_tag_payload) {
904c6c70c07SAlexander Motin 		bus_dma_tag_destroy(qpair->dma_tag_payload);
905550d5d64SAlexander Motin 		qpair->dma_tag_payload = NULL;
906550d5d64SAlexander Motin 	}
907550d5d64SAlexander Motin 
908550d5d64SAlexander Motin 	if (mtx_initialized(&qpair->lock))
909550d5d64SAlexander Motin 		mtx_destroy(&qpair->lock);
910550d5d64SAlexander Motin 
911550d5d64SAlexander Motin 	if (qpair->res) {
912550d5d64SAlexander Motin 		bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ,
913550d5d64SAlexander Motin 		    rman_get_rid(qpair->res), qpair->res);
914550d5d64SAlexander Motin 		qpair->res = NULL;
915550d5d64SAlexander Motin 	}
916bb0ec6b3SJim Harris }
917bb0ec6b3SJim Harris 
918b846efd7SJim Harris static void
919b846efd7SJim Harris nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair)
920b846efd7SJim Harris {
921b846efd7SJim Harris 	struct nvme_tracker	*tr;
922b846efd7SJim Harris 
923b846efd7SJim Harris 	tr = TAILQ_FIRST(&qpair->outstanding_tr);
924b846efd7SJim Harris 	while (tr != NULL) {
9259544e6dcSChuck Tuffli 		if (tr->req->cmd.opc == NVME_OPC_ASYNC_EVENT_REQUEST) {
92643393e8bSWarner Losh 			nvme_qpair_manual_complete_tracker(tr,
927232e2edbSJim Harris 			    NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0,
9282ffd6fceSWarner Losh 			    ERROR_PRINT_NONE);
929b846efd7SJim Harris 			tr = TAILQ_FIRST(&qpair->outstanding_tr);
930b846efd7SJim Harris 		} else {
931b846efd7SJim Harris 			tr = TAILQ_NEXT(tr, tailq);
932b846efd7SJim Harris 		}
933b846efd7SJim Harris 	}
934b846efd7SJim Harris }
935b846efd7SJim Harris 
936bb0ec6b3SJim Harris void
937bb0ec6b3SJim Harris nvme_admin_qpair_destroy(struct nvme_qpair *qpair)
938bb0ec6b3SJim Harris {
939bb0ec6b3SJim Harris 
940b846efd7SJim Harris 	nvme_admin_qpair_abort_aers(qpair);
941bb0ec6b3SJim Harris 	nvme_qpair_destroy(qpair);
942bb0ec6b3SJim Harris }
943bb0ec6b3SJim Harris 
944bb0ec6b3SJim Harris void
945bb0ec6b3SJim Harris nvme_io_qpair_destroy(struct nvme_qpair *qpair)
946bb0ec6b3SJim Harris {
947bb0ec6b3SJim Harris 
948bb0ec6b3SJim Harris 	nvme_qpair_destroy(qpair);
949bb0ec6b3SJim Harris }
950bb0ec6b3SJim Harris 
951bb0ec6b3SJim Harris static void
952502dc84aSWarner Losh nvme_qpair_timeout(void *arg)
9530a0b08ccSJim Harris {
954502dc84aSWarner Losh 	struct nvme_qpair	*qpair = arg;
95512d191ecSJim Harris 	struct nvme_controller	*ctrlr = qpair->ctrlr;
956502dc84aSWarner Losh 	struct nvme_tracker	*tr;
957502dc84aSWarner Losh 	sbintime_t		now;
958502dc84aSWarner Losh 	bool			idle;
9590d787e9bSWojciech Macek 	uint32_t		csts;
9600d787e9bSWojciech Macek 	uint8_t			cfs;
961448195e7SJim Harris 
962502dc84aSWarner Losh 	mtx_lock(&qpair->lock);
963502dc84aSWarner Losh 	idle = TAILQ_EMPTY(&qpair->outstanding_tr);
964502dc84aSWarner Losh again:
965502dc84aSWarner Losh 	switch (qpair->recovery_state) {
966502dc84aSWarner Losh 	case RECOVERY_NONE:
967502dc84aSWarner Losh 		if (idle)
968502dc84aSWarner Losh 			break;
969502dc84aSWarner Losh 		now = getsbinuptime();
970b3c9b606SAlexander Motin 		idle = true;
971b3c9b606SAlexander Motin 		TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq) {
972b3c9b606SAlexander Motin 			if (tr->deadline == SBT_MAX)
973b3c9b606SAlexander Motin 				continue;
974b3c9b606SAlexander Motin 			idle = false;
975b3c9b606SAlexander Motin 			if (now > tr->deadline) {
976502dc84aSWarner Losh 				/*
977502dc84aSWarner Losh 				 * We're now passed our earliest deadline. We
978502dc84aSWarner Losh 				 * need to do expensive things to cope, but next
979502dc84aSWarner Losh 				 * time. Flag that and close the door to any
980502dc84aSWarner Losh 				 * further processing.
981502dc84aSWarner Losh 				 */
982502dc84aSWarner Losh 				qpair->recovery_state = RECOVERY_START;
983502dc84aSWarner Losh 				nvme_printf(ctrlr, "RECOVERY_START %jd vs %jd\n",
984502dc84aSWarner Losh 				    (uintmax_t)now, (uintmax_t)tr->deadline);
985502dc84aSWarner Losh 				break;
986502dc84aSWarner Losh 			}
987502dc84aSWarner Losh 		}
988502dc84aSWarner Losh 		break;
989502dc84aSWarner Losh 	case RECOVERY_START:
99048ce3178SJim Harris 		/*
991d85d9648SWarner Losh 		 * Read csts to get value of cfs - controller fatal status.
992d85d9648SWarner Losh 		 * If no fatal status, try to call the completion routine, and
993d85d9648SWarner Losh 		 * if completes transactions, report a missed interrupt and
994d85d9648SWarner Losh 		 * return (this may need to be rate limited). Otherwise, if
995d85d9648SWarner Losh 		 * aborts are enabled and the controller is not reporting
996d85d9648SWarner Losh 		 * fatal status, abort the command. Otherwise, just reset the
997d85d9648SWarner Losh 		 * controller and hope for the best.
99848ce3178SJim Harris 		 */
999d85d9648SWarner Losh 		csts = nvme_mmio_read_4(ctrlr, csts);
1000d85d9648SWarner Losh 		cfs = (csts >> NVME_CSTS_REG_CFS_SHIFT) & NVME_CSTS_REG_CFS_MASK;
1001502dc84aSWarner Losh 		if (cfs) {
1002502dc84aSWarner Losh 			nvme_printf(ctrlr, "Controller in fatal status, resetting\n");
1003502dc84aSWarner Losh 			qpair->recovery_state = RECOVERY_RESET;
1004502dc84aSWarner Losh 			goto again;
1005d85d9648SWarner Losh 		}
1006502dc84aSWarner Losh 		mtx_unlock(&qpair->lock);
1007502dc84aSWarner Losh 		if (nvme_qpair_process_completions(qpair)) {
1008502dc84aSWarner Losh 			nvme_printf(ctrlr, "Completions present in output without an interrupt\n");
1009502dc84aSWarner Losh 			qpair->recovery_state = RECOVERY_NONE;
1010d85d9648SWarner Losh 		} else {
1011502dc84aSWarner Losh 			nvme_printf(ctrlr, "timeout with nothing complete, resetting\n");
1012502dc84aSWarner Losh 			qpair->recovery_state = RECOVERY_RESET;
1013502dc84aSWarner Losh 			mtx_lock(&qpair->lock);
1014502dc84aSWarner Losh 			goto again;
1015bb0ec6b3SJim Harris 		}
1016502dc84aSWarner Losh 		mtx_lock(&qpair->lock);
1017502dc84aSWarner Losh 		break;
1018502dc84aSWarner Losh 	case RECOVERY_RESET:
1019502dc84aSWarner Losh 		/*
1020502dc84aSWarner Losh 		 * If we get here due to a possible surprise hot-unplug event,
1021502dc84aSWarner Losh 		 * then we let nvme_ctrlr_reset confirm and fail the
1022502dc84aSWarner Losh 		 * controller.
1023502dc84aSWarner Losh 		 */
1024502dc84aSWarner Losh 		nvme_printf(ctrlr, "Resetting controller due to a timeout%s.\n",
102518dc12bfSWarner Losh 		    (csts == 0xffffffff) ? " and possible hot unplug" :
102618dc12bfSWarner Losh 		    (cfs ? " and fatal error status" : ""));
1027502dc84aSWarner Losh 		nvme_printf(ctrlr, "RECOVERY_WAITING\n");
1028502dc84aSWarner Losh 		qpair->recovery_state = RECOVERY_WAITING;
1029502dc84aSWarner Losh 		nvme_ctrlr_reset(ctrlr);
1030502dc84aSWarner Losh 		break;
1031502dc84aSWarner Losh 	case RECOVERY_WAITING:
1032502dc84aSWarner Losh 		nvme_printf(ctrlr, "waiting\n");
1033502dc84aSWarner Losh 		break;
1034d85d9648SWarner Losh 	}
1035bb0ec6b3SJim Harris 
1036502dc84aSWarner Losh 	/*
1037502dc84aSWarner Losh 	 * Rearm the timeout.
1038502dc84aSWarner Losh 	 */
1039502dc84aSWarner Losh 	if (!idle) {
1040b3c9b606SAlexander Motin 		callout_schedule_sbt(&qpair->timer, SBT_1S / 2, SBT_1S / 2, 0);
1041502dc84aSWarner Losh 	} else {
1042502dc84aSWarner Losh 		qpair->timer_armed = false;
1043502dc84aSWarner Losh 	}
1044502dc84aSWarner Losh 	mtx_unlock(&qpair->lock);
1045502dc84aSWarner Losh }
1046502dc84aSWarner Losh 
1047502dc84aSWarner Losh /*
1048502dc84aSWarner Losh  * Submit the tracker to the hardware. Must already be in the
1049502dc84aSWarner Losh  * outstanding queue when called.
1050502dc84aSWarner Losh  */
1051bb0ec6b3SJim Harris void
1052b846efd7SJim Harris nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr)
1053bb0ec6b3SJim Harris {
1054ad697276SJim Harris 	struct nvme_request	*req;
105594143332SJim Harris 	struct nvme_controller	*ctrlr;
1056ead7e103SAlexander Motin 	int timeout;
1057bb0ec6b3SJim Harris 
1058b846efd7SJim Harris 	mtx_assert(&qpair->lock, MA_OWNED);
1059b846efd7SJim Harris 
1060ad697276SJim Harris 	req = tr->req;
1061ad697276SJim Harris 	req->cmd.cid = tr->cid;
1062bb0ec6b3SJim Harris 	qpair->act_tr[tr->cid] = tr;
106394143332SJim Harris 	ctrlr = qpair->ctrlr;
1064bb0ec6b3SJim Harris 
1065ead7e103SAlexander Motin 	if (req->timeout) {
1066ead7e103SAlexander Motin 		if (req->cb_fn == nvme_completion_poll_cb)
1067502dc84aSWarner Losh 			timeout = 1;
1068ead7e103SAlexander Motin 		else
1069502dc84aSWarner Losh 			timeout = ctrlr->timeout_period;
1070502dc84aSWarner Losh 		tr->deadline = getsbinuptime() + timeout * SBT_1S;
1071502dc84aSWarner Losh 		if (!qpair->timer_armed) {
1072502dc84aSWarner Losh 			qpair->timer_armed = true;
1073b3c9b606SAlexander Motin 			callout_reset_sbt_on(&qpair->timer, SBT_1S / 2, SBT_1S / 2,
1074b3c9b606SAlexander Motin 			    nvme_qpair_timeout, qpair, qpair->cpu, 0);
1075ead7e103SAlexander Motin 		}
1076502dc84aSWarner Losh 	} else
1077502dc84aSWarner Losh 		tr->deadline = SBT_MAX;
1078bb0ec6b3SJim Harris 
1079bb0ec6b3SJim Harris 	/* Copy the command from the tracker to the submission queue. */
1080ad697276SJim Harris 	memcpy(&qpair->cmd[qpair->sq_tail], &req->cmd, sizeof(req->cmd));
1081bb0ec6b3SJim Harris 
1082bb0ec6b3SJim Harris 	if (++qpair->sq_tail == qpair->num_entries)
1083bb0ec6b3SJim Harris 		qpair->sq_tail = 0;
1084bb0ec6b3SJim Harris 
10852e0090afSJustin Hibbits 	bus_dmamap_sync(qpair->dma_tag, qpair->queuemem_map,
10862e0090afSJustin Hibbits 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1087f93b7f95SWarner Losh 	bus_space_write_4(qpair->ctrlr->bus_tag, qpair->ctrlr->bus_handle,
1088f93b7f95SWarner Losh 	    qpair->sq_tdbl_off, qpair->sq_tail);
1089bb0ec6b3SJim Harris 	qpair->num_cmds++;
1090bb0ec6b3SJim Harris }
10915ae9ed68SJim Harris 
1092d6f54866SJim Harris static void
1093ca269f32SJim Harris nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1094ca269f32SJim Harris {
1095ca269f32SJim Harris 	struct nvme_tracker 	*tr = arg;
1096ca269f32SJim Harris 	uint32_t		cur_nseg;
1097ca269f32SJim Harris 
1098ca269f32SJim Harris 	/*
1099ca269f32SJim Harris 	 * If the mapping operation failed, return immediately.  The caller
1100ca269f32SJim Harris 	 *  is responsible for detecting the error status and failing the
1101ca269f32SJim Harris 	 *  tracker manually.
1102ca269f32SJim Harris 	 */
1103a6e30963SJim Harris 	if (error != 0) {
1104a6e30963SJim Harris 		nvme_printf(tr->qpair->ctrlr,
1105a6e30963SJim Harris 		    "nvme_payload_map err %d\n", error);
1106ca269f32SJim Harris 		return;
1107a6e30963SJim Harris 	}
1108ca269f32SJim Harris 
1109ca269f32SJim Harris 	/*
11100fd4cd40SWarner Losh 	 * Note that we specified ctrlr->page_size for alignment and max
11110fd4cd40SWarner Losh 	 * segment size when creating the bus dma tags.  So here we can safely
11120fd4cd40SWarner Losh 	 * just transfer each segment to its associated PRP entry.
1113ca269f32SJim Harris 	 */
11140d787e9bSWojciech Macek 	tr->req->cmd.prp1 = htole64(seg[0].ds_addr);
1115ca269f32SJim Harris 
1116ca269f32SJim Harris 	if (nseg == 2) {
11170d787e9bSWojciech Macek 		tr->req->cmd.prp2 = htole64(seg[1].ds_addr);
1118ca269f32SJim Harris 	} else if (nseg > 2) {
1119ca269f32SJim Harris 		cur_nseg = 1;
11200d787e9bSWojciech Macek 		tr->req->cmd.prp2 = htole64((uint64_t)tr->prp_bus_addr);
1121ca269f32SJim Harris 		while (cur_nseg < nseg) {
1122ca269f32SJim Harris 			tr->prp[cur_nseg-1] =
11230d787e9bSWojciech Macek 			    htole64((uint64_t)seg[cur_nseg].ds_addr);
1124ca269f32SJim Harris 			cur_nseg++;
1125ca269f32SJim Harris 		}
1126a6e30963SJim Harris 	} else {
1127a6e30963SJim Harris 		/*
1128a6e30963SJim Harris 		 * prp2 should not be used by the controller
1129a6e30963SJim Harris 		 *  since there is only one segment, but set
1130a6e30963SJim Harris 		 *  to 0 just to be safe.
1131a6e30963SJim Harris 		 */
1132a6e30963SJim Harris 		tr->req->cmd.prp2 = 0;
1133ca269f32SJim Harris 	}
1134ca269f32SJim Harris 
11352e0090afSJustin Hibbits 	bus_dmamap_sync(tr->qpair->dma_tag_payload, tr->payload_dma_map,
11362e0090afSJustin Hibbits 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1137ca269f32SJim Harris 	nvme_qpair_submit_tracker(tr->qpair, tr);
1138ca269f32SJim Harris }
1139ca269f32SJim Harris 
1140ca269f32SJim Harris static void
1141d6f54866SJim Harris _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
11425ae9ed68SJim Harris {
11435ae9ed68SJim Harris 	struct nvme_tracker	*tr;
1144e2b99004SJim Harris 	int			err = 0;
11455ae9ed68SJim Harris 
1146d6f54866SJim Harris 	mtx_assert(&qpair->lock, MA_OWNED);
11475ae9ed68SJim Harris 
114865c2474eSJim Harris 	tr = TAILQ_FIRST(&qpair->free_tr);
1149232e2edbSJim Harris 	req->qpair = qpair;
115021b6da58SJim Harris 
1151502dc84aSWarner Losh 	if (tr == NULL || qpair->recovery_state != RECOVERY_NONE) {
11520f71ecf7SJim Harris 		/*
1153b846efd7SJim Harris 		 * No tracker is available, or the qpair is disabled due to
1154232e2edbSJim Harris 		 *  an in-progress controller-level reset or controller
1155232e2edbSJim Harris 		 *  failure.
1156232e2edbSJim Harris 		 */
1157232e2edbSJim Harris 
1158232e2edbSJim Harris 		if (qpair->ctrlr->is_failed) {
1159232e2edbSJim Harris 			/*
11604b977e6dSWarner Losh 			 * The controller has failed, so fail the request.
1161232e2edbSJim Harris 			 */
11624b977e6dSWarner Losh 			nvme_qpair_manual_complete_request(qpair, req,
11634b977e6dSWarner Losh 			    NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST);
1164232e2edbSJim Harris 		} else {
1165232e2edbSJim Harris 			/*
1166232e2edbSJim Harris 			 * Put the request on the qpair's request queue to be
1167232e2edbSJim Harris 			 *  processed when a tracker frees up via a command
1168232e2edbSJim Harris 			 *  completion or when the controller reset is
1169232e2edbSJim Harris 			 *  completed.
11700f71ecf7SJim Harris 			 */
11710f71ecf7SJim Harris 			STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq);
1172232e2edbSJim Harris 		}
1173d6f54866SJim Harris 		return;
117421b6da58SJim Harris 	}
117521b6da58SJim Harris 
117665c2474eSJim Harris 	TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
117765c2474eSJim Harris 	TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq);
1178502dc84aSWarner Losh 	if (!qpair->timer_armed)
1179502dc84aSWarner Losh 		tr->deadline = SBT_MAX;
11805ae9ed68SJim Harris 	tr->req = req;
11815ae9ed68SJim Harris 
11821e526bc4SJim Harris 	switch (req->type) {
11831e526bc4SJim Harris 	case NVME_REQUEST_VADDR:
11847b68ae1eSJim Harris 		KASSERT(req->payload_size <= qpair->ctrlr->max_xfer_size,
11857b68ae1eSJim Harris 		    ("payload_size (%d) exceeds max_xfer_size (%d)\n",
11867b68ae1eSJim Harris 		    req->payload_size, qpair->ctrlr->max_xfer_size));
1187a6e30963SJim Harris 		err = bus_dmamap_load(tr->qpair->dma_tag_payload,
1188a6e30963SJim Harris 		    tr->payload_dma_map, req->u.payload, req->payload_size,
1189a6e30963SJim Harris 		    nvme_payload_map, tr, 0);
11905ae9ed68SJim Harris 		if (err != 0)
1191e2b99004SJim Harris 			nvme_printf(qpair->ctrlr,
1192e2b99004SJim Harris 			    "bus_dmamap_load returned 0x%x!\n", err);
11931e526bc4SJim Harris 		break;
11941e526bc4SJim Harris 	case NVME_REQUEST_NULL:
1195b846efd7SJim Harris 		nvme_qpair_submit_tracker(tr->qpair, tr);
11961e526bc4SJim Harris 		break;
11975fdf9c3cSJim Harris 	case NVME_REQUEST_BIO:
11987b68ae1eSJim Harris 		KASSERT(req->u.bio->bio_bcount <= qpair->ctrlr->max_xfer_size,
11997b68ae1eSJim Harris 		    ("bio->bio_bcount (%jd) exceeds max_xfer_size (%d)\n",
12007b68ae1eSJim Harris 		    (intmax_t)req->u.bio->bio_bcount,
12017b68ae1eSJim Harris 		    qpair->ctrlr->max_xfer_size));
1202a6e30963SJim Harris 		err = bus_dmamap_load_bio(tr->qpair->dma_tag_payload,
12035fdf9c3cSJim Harris 		    tr->payload_dma_map, req->u.bio, nvme_payload_map, tr, 0);
12045fdf9c3cSJim Harris 		if (err != 0)
1205e2b99004SJim Harris 			nvme_printf(qpair->ctrlr,
1206e2b99004SJim Harris 			    "bus_dmamap_load_bio returned 0x%x!\n", err);
12075fdf9c3cSJim Harris 		break;
120851977281SWarner Losh 	case NVME_REQUEST_CCB:
120951977281SWarner Losh 		err = bus_dmamap_load_ccb(tr->qpair->dma_tag_payload,
121051977281SWarner Losh 		    tr->payload_dma_map, req->u.payload,
121151977281SWarner Losh 		    nvme_payload_map, tr, 0);
121251977281SWarner Losh 		if (err != 0)
121351977281SWarner Losh 			nvme_printf(qpair->ctrlr,
121451977281SWarner Losh 			    "bus_dmamap_load_ccb returned 0x%x!\n", err);
121551977281SWarner Losh 		break;
12161e526bc4SJim Harris 	default:
12171e526bc4SJim Harris 		panic("unknown nvme request type 0x%x\n", req->type);
12181e526bc4SJim Harris 		break;
12195ae9ed68SJim Harris 	}
1220e2b99004SJim Harris 
1221e2b99004SJim Harris 	if (err != 0) {
1222e2b99004SJim Harris 		/*
1223e2b99004SJim Harris 		 * The dmamap operation failed, so we manually fail the
1224e2b99004SJim Harris 		 *  tracker here with DATA_TRANSFER_ERROR status.
1225e2b99004SJim Harris 		 *
1226e2b99004SJim Harris 		 * nvme_qpair_manual_complete_tracker must not be called
1227e2b99004SJim Harris 		 *  with the qpair lock held.
1228e2b99004SJim Harris 		 */
1229e2b99004SJim Harris 		mtx_unlock(&qpair->lock);
123043393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
12312ffd6fceSWarner Losh 		    NVME_SC_DATA_TRANSFER_ERROR, DO_NOT_RETRY, ERROR_PRINT_ALL);
1232e2b99004SJim Harris 		mtx_lock(&qpair->lock);
1233e2b99004SJim Harris 	}
1234d6f54866SJim Harris }
12355ae9ed68SJim Harris 
1236d6f54866SJim Harris void
1237d6f54866SJim Harris nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
1238d6f54866SJim Harris {
1239d6f54866SJim Harris 
1240d6f54866SJim Harris 	mtx_lock(&qpair->lock);
1241d6f54866SJim Harris 	_nvme_qpair_submit_request(qpair, req);
12425ae9ed68SJim Harris 	mtx_unlock(&qpair->lock);
12435ae9ed68SJim Harris }
1244b846efd7SJim Harris 
1245b846efd7SJim Harris static void
1246b846efd7SJim Harris nvme_qpair_enable(struct nvme_qpair *qpair)
1247b846efd7SJim Harris {
1248502dc84aSWarner Losh 	mtx_assert(&qpair->lock, MA_OWNED);
1249b846efd7SJim Harris 
1250502dc84aSWarner Losh 	qpair->recovery_state = RECOVERY_NONE;
1251cb5b7c13SJim Harris }
1252cb5b7c13SJim Harris 
1253cb5b7c13SJim Harris void
1254cb5b7c13SJim Harris nvme_qpair_reset(struct nvme_qpair *qpair)
1255cb5b7c13SJim Harris {
1256cb5b7c13SJim Harris 
1257b846efd7SJim Harris 	qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0;
1258b846efd7SJim Harris 
1259b846efd7SJim Harris 	/*
1260b846efd7SJim Harris 	 * First time through the completion queue, HW will set phase
1261b846efd7SJim Harris 	 *  bit on completions to 1.  So set this to 1 here, indicating
1262b846efd7SJim Harris 	 *  we're looking for a 1 to know which entries have completed.
1263b846efd7SJim Harris 	 *  we'll toggle the bit each time when the completion queue
1264b846efd7SJim Harris 	 *  rolls over.
1265b846efd7SJim Harris 	 */
1266b846efd7SJim Harris 	qpair->phase = 1;
1267b846efd7SJim Harris 
1268b846efd7SJim Harris 	memset(qpair->cmd, 0,
1269b846efd7SJim Harris 	    qpair->num_entries * sizeof(struct nvme_command));
1270b846efd7SJim Harris 	memset(qpair->cpl, 0,
1271b846efd7SJim Harris 	    qpair->num_entries * sizeof(struct nvme_completion));
1272b846efd7SJim Harris }
1273b846efd7SJim Harris 
1274b846efd7SJim Harris void
1275b846efd7SJim Harris nvme_admin_qpair_enable(struct nvme_qpair *qpair)
1276b846efd7SJim Harris {
127743a37256SJim Harris 	struct nvme_tracker		*tr;
127843a37256SJim Harris 	struct nvme_tracker		*tr_temp;
127943a37256SJim Harris 
128043a37256SJim Harris 	/*
128143a37256SJim Harris 	 * Manually abort each outstanding admin command.  Do not retry
128243a37256SJim Harris 	 *  admin commands found here, since they will be left over from
128343a37256SJim Harris 	 *  a controller reset and its likely the context in which the
128443a37256SJim Harris 	 *  command was issued no longer applies.
128543a37256SJim Harris 	 */
128643a37256SJim Harris 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1287547d523eSJim Harris 		nvme_printf(qpair->ctrlr,
128843a37256SJim Harris 		    "aborting outstanding admin command\n");
128943393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
12902ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST, DO_NOT_RETRY, ERROR_PRINT_ALL);
129143a37256SJim Harris 	}
1292b846efd7SJim Harris 
1293502dc84aSWarner Losh 	mtx_lock(&qpair->lock);
1294b846efd7SJim Harris 	nvme_qpair_enable(qpair);
1295502dc84aSWarner Losh 	mtx_unlock(&qpair->lock);
1296b846efd7SJim Harris }
1297b846efd7SJim Harris 
1298b846efd7SJim Harris void
1299b846efd7SJim Harris nvme_io_qpair_enable(struct nvme_qpair *qpair)
1300b846efd7SJim Harris {
1301b846efd7SJim Harris 	STAILQ_HEAD(, nvme_request)	temp;
1302b846efd7SJim Harris 	struct nvme_tracker		*tr;
1303cb5b7c13SJim Harris 	struct nvme_tracker		*tr_temp;
1304b846efd7SJim Harris 	struct nvme_request		*req;
1305b846efd7SJim Harris 
1306cb5b7c13SJim Harris 	/*
1307cb5b7c13SJim Harris 	 * Manually abort each outstanding I/O.  This normally results in a
1308cb5b7c13SJim Harris 	 *  retry, unless the retry count on the associated request has
1309cb5b7c13SJim Harris 	 *  reached its limit.
1310cb5b7c13SJim Harris 	 */
1311cb5b7c13SJim Harris 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1312547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n");
131343393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
13142ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST, 0, ERROR_PRINT_NO_RETRY);
1315cb5b7c13SJim Harris 	}
1316cb5b7c13SJim Harris 
1317b846efd7SJim Harris 	mtx_lock(&qpair->lock);
1318b846efd7SJim Harris 
1319b846efd7SJim Harris 	nvme_qpair_enable(qpair);
1320b846efd7SJim Harris 
1321b846efd7SJim Harris 	STAILQ_INIT(&temp);
1322b846efd7SJim Harris 	STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request);
1323b846efd7SJim Harris 
1324b846efd7SJim Harris 	while (!STAILQ_EMPTY(&temp)) {
1325b846efd7SJim Harris 		req = STAILQ_FIRST(&temp);
1326b846efd7SJim Harris 		STAILQ_REMOVE_HEAD(&temp, stailq);
1327547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n");
1328547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
1329b846efd7SJim Harris 		_nvme_qpair_submit_request(qpair, req);
1330b846efd7SJim Harris 	}
1331b846efd7SJim Harris 
1332b846efd7SJim Harris 	mtx_unlock(&qpair->lock);
1333b846efd7SJim Harris }
1334b846efd7SJim Harris 
1335b846efd7SJim Harris static void
1336b846efd7SJim Harris nvme_qpair_disable(struct nvme_qpair *qpair)
1337b846efd7SJim Harris {
1338502dc84aSWarner Losh 	struct nvme_tracker	*tr, *tr_temp;
1339b846efd7SJim Harris 
1340b846efd7SJim Harris 	mtx_lock(&qpair->lock);
1341502dc84aSWarner Losh 	qpair->recovery_state = RECOVERY_WAITING;
1342502dc84aSWarner Losh 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
1343502dc84aSWarner Losh 		tr->deadline = SBT_MAX;
1344502dc84aSWarner Losh 	}
1345b846efd7SJim Harris 	mtx_unlock(&qpair->lock);
1346b846efd7SJim Harris }
1347b846efd7SJim Harris 
1348b846efd7SJim Harris void
1349b846efd7SJim Harris nvme_admin_qpair_disable(struct nvme_qpair *qpair)
1350b846efd7SJim Harris {
1351b846efd7SJim Harris 
1352b846efd7SJim Harris 	nvme_qpair_disable(qpair);
1353b846efd7SJim Harris 	nvme_admin_qpair_abort_aers(qpair);
1354b846efd7SJim Harris }
1355b846efd7SJim Harris 
1356b846efd7SJim Harris void
1357b846efd7SJim Harris nvme_io_qpair_disable(struct nvme_qpair *qpair)
1358b846efd7SJim Harris {
1359b846efd7SJim Harris 
1360b846efd7SJim Harris 	nvme_qpair_disable(qpair);
1361b846efd7SJim Harris }
1362232e2edbSJim Harris 
1363232e2edbSJim Harris void
1364232e2edbSJim Harris nvme_qpair_fail(struct nvme_qpair *qpair)
1365232e2edbSJim Harris {
1366232e2edbSJim Harris 	struct nvme_tracker		*tr;
1367232e2edbSJim Harris 	struct nvme_request		*req;
1368232e2edbSJim Harris 
1369824073fbSWarner Losh 	if (!mtx_initialized(&qpair->lock))
1370824073fbSWarner Losh 		return;
1371824073fbSWarner Losh 
1372232e2edbSJim Harris 	mtx_lock(&qpair->lock);
1373232e2edbSJim Harris 
1374232e2edbSJim Harris 	while (!STAILQ_EMPTY(&qpair->queued_req)) {
1375232e2edbSJim Harris 		req = STAILQ_FIRST(&qpair->queued_req);
1376232e2edbSJim Harris 		STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
1377547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "failing queued i/o\n");
1378232e2edbSJim Harris 		mtx_unlock(&qpair->lock);
1379232e2edbSJim Harris 		nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC,
13802ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST);
1381232e2edbSJim Harris 		mtx_lock(&qpair->lock);
1382232e2edbSJim Harris 	}
1383232e2edbSJim Harris 
1384232e2edbSJim Harris 	/* Manually abort each outstanding I/O. */
1385232e2edbSJim Harris 	while (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
1386232e2edbSJim Harris 		tr = TAILQ_FIRST(&qpair->outstanding_tr);
1387232e2edbSJim Harris 		/*
1388232e2edbSJim Harris 		 * Do not remove the tracker.  The abort_tracker path will
1389232e2edbSJim Harris 		 *  do that for us.
1390232e2edbSJim Harris 		 */
1391547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "failing outstanding i/o\n");
1392232e2edbSJim Harris 		mtx_unlock(&qpair->lock);
139343393e8bSWarner Losh 		nvme_qpair_manual_complete_tracker(tr, NVME_SCT_GENERIC,
13942ffd6fceSWarner Losh 		    NVME_SC_ABORTED_BY_REQUEST, DO_NOT_RETRY, ERROR_PRINT_ALL);
1395232e2edbSJim Harris 		mtx_lock(&qpair->lock);
1396232e2edbSJim Harris 	}
1397232e2edbSJim Harris 
1398232e2edbSJim Harris 	mtx_unlock(&qpair->lock);
1399232e2edbSJim Harris }
1400