xref: /freebsd/sys/dev/nvme/nvme_qpair.c (revision 824073fbd6aa0740b6f4f1ba8d1112a0a9fdb6ad)
1bb0ec6b3SJim Harris /*-
2f42ca756SJim Harris  * Copyright (C) 2012-2014 Intel Corporation
3bb0ec6b3SJim Harris  * All rights reserved.
4bb0ec6b3SJim Harris  *
5bb0ec6b3SJim Harris  * Redistribution and use in source and binary forms, with or without
6bb0ec6b3SJim Harris  * modification, are permitted provided that the following conditions
7bb0ec6b3SJim Harris  * are met:
8bb0ec6b3SJim Harris  * 1. Redistributions of source code must retain the above copyright
9bb0ec6b3SJim Harris  *    notice, this list of conditions and the following disclaimer.
10bb0ec6b3SJim Harris  * 2. Redistributions in binary form must reproduce the above copyright
11bb0ec6b3SJim Harris  *    notice, this list of conditions and the following disclaimer in the
12bb0ec6b3SJim Harris  *    documentation and/or other materials provided with the distribution.
13bb0ec6b3SJim Harris  *
14bb0ec6b3SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15bb0ec6b3SJim Harris  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16bb0ec6b3SJim Harris  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17bb0ec6b3SJim Harris  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18bb0ec6b3SJim Harris  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19bb0ec6b3SJim Harris  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20bb0ec6b3SJim Harris  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21bb0ec6b3SJim Harris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22bb0ec6b3SJim Harris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23bb0ec6b3SJim Harris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24bb0ec6b3SJim Harris  * SUCH DAMAGE.
25bb0ec6b3SJim Harris  */
26bb0ec6b3SJim Harris 
27bb0ec6b3SJim Harris #include <sys/cdefs.h>
28bb0ec6b3SJim Harris __FBSDID("$FreeBSD$");
29bb0ec6b3SJim Harris 
30bb0ec6b3SJim Harris #include <sys/param.h>
31bb0ec6b3SJim Harris #include <sys/bus.h>
32bb0ec6b3SJim Harris 
330f71ecf7SJim Harris #include <dev/pci/pcivar.h>
340f71ecf7SJim Harris 
35bb0ec6b3SJim Harris #include "nvme_private.h"
36bb0ec6b3SJim Harris 
37d6f54866SJim Harris static void	_nvme_qpair_submit_request(struct nvme_qpair *qpair,
38d6f54866SJim Harris 					   struct nvme_request *req);
39a965389bSScott Long static void	nvme_qpair_destroy(struct nvme_qpair *qpair);
40d6f54866SJim Harris 
41547d523eSJim Harris struct nvme_opcode_string {
42547d523eSJim Harris 
43547d523eSJim Harris 	uint16_t	opc;
44547d523eSJim Harris 	const char *	str;
45547d523eSJim Harris };
46547d523eSJim Harris 
47547d523eSJim Harris static struct nvme_opcode_string admin_opcode[] = {
48547d523eSJim Harris 	{ NVME_OPC_DELETE_IO_SQ, "DELETE IO SQ" },
49547d523eSJim Harris 	{ NVME_OPC_CREATE_IO_SQ, "CREATE IO SQ" },
50547d523eSJim Harris 	{ NVME_OPC_GET_LOG_PAGE, "GET LOG PAGE" },
51547d523eSJim Harris 	{ NVME_OPC_DELETE_IO_CQ, "DELETE IO CQ" },
52547d523eSJim Harris 	{ NVME_OPC_CREATE_IO_CQ, "CREATE IO CQ" },
53547d523eSJim Harris 	{ NVME_OPC_IDENTIFY, "IDENTIFY" },
54547d523eSJim Harris 	{ NVME_OPC_ABORT, "ABORT" },
55547d523eSJim Harris 	{ NVME_OPC_SET_FEATURES, "SET FEATURES" },
56547d523eSJim Harris 	{ NVME_OPC_GET_FEATURES, "GET FEATURES" },
57547d523eSJim Harris 	{ NVME_OPC_ASYNC_EVENT_REQUEST, "ASYNC EVENT REQUEST" },
58547d523eSJim Harris 	{ NVME_OPC_FIRMWARE_ACTIVATE, "FIRMWARE ACTIVATE" },
59547d523eSJim Harris 	{ NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD, "FIRMWARE IMAGE DOWNLOAD" },
60547d523eSJim Harris 	{ NVME_OPC_FORMAT_NVM, "FORMAT NVM" },
61547d523eSJim Harris 	{ NVME_OPC_SECURITY_SEND, "SECURITY SEND" },
62547d523eSJim Harris 	{ NVME_OPC_SECURITY_RECEIVE, "SECURITY RECEIVE" },
63547d523eSJim Harris 	{ 0xFFFF, "ADMIN COMMAND" }
64547d523eSJim Harris };
65547d523eSJim Harris 
66547d523eSJim Harris static struct nvme_opcode_string io_opcode[] = {
67547d523eSJim Harris 	{ NVME_OPC_FLUSH, "FLUSH" },
68547d523eSJim Harris 	{ NVME_OPC_WRITE, "WRITE" },
69547d523eSJim Harris 	{ NVME_OPC_READ, "READ" },
70547d523eSJim Harris 	{ NVME_OPC_WRITE_UNCORRECTABLE, "WRITE UNCORRECTABLE" },
71547d523eSJim Harris 	{ NVME_OPC_COMPARE, "COMPARE" },
72547d523eSJim Harris 	{ NVME_OPC_DATASET_MANAGEMENT, "DATASET MANAGEMENT" },
73547d523eSJim Harris 	{ 0xFFFF, "IO COMMAND" }
74547d523eSJim Harris };
75547d523eSJim Harris 
76547d523eSJim Harris static const char *
77547d523eSJim Harris get_admin_opcode_string(uint16_t opc)
78547d523eSJim Harris {
79547d523eSJim Harris 	struct nvme_opcode_string *entry;
80547d523eSJim Harris 
81547d523eSJim Harris 	entry = admin_opcode;
82547d523eSJim Harris 
83547d523eSJim Harris 	while (entry->opc != 0xFFFF) {
84547d523eSJim Harris 		if (entry->opc == opc)
85547d523eSJim Harris 			return (entry->str);
86547d523eSJim Harris 		entry++;
87547d523eSJim Harris 	}
88547d523eSJim Harris 	return (entry->str);
89547d523eSJim Harris }
90547d523eSJim Harris 
91547d523eSJim Harris static const char *
92547d523eSJim Harris get_io_opcode_string(uint16_t opc)
93547d523eSJim Harris {
94547d523eSJim Harris 	struct nvme_opcode_string *entry;
95547d523eSJim Harris 
96547d523eSJim Harris 	entry = io_opcode;
97547d523eSJim Harris 
98547d523eSJim Harris 	while (entry->opc != 0xFFFF) {
99547d523eSJim Harris 		if (entry->opc == opc)
100547d523eSJim Harris 			return (entry->str);
101547d523eSJim Harris 		entry++;
102547d523eSJim Harris 	}
103547d523eSJim Harris 	return (entry->str);
104547d523eSJim Harris }
105547d523eSJim Harris 
106547d523eSJim Harris 
107547d523eSJim Harris static void
108547d523eSJim Harris nvme_admin_qpair_print_command(struct nvme_qpair *qpair,
109547d523eSJim Harris     struct nvme_command *cmd)
110547d523eSJim Harris {
111547d523eSJim Harris 
112547d523eSJim Harris 	nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%x "
113547d523eSJim Harris 	    "cdw10:%08x cdw11:%08x\n",
114547d523eSJim Harris 	    get_admin_opcode_string(cmd->opc), cmd->opc, qpair->id, cmd->cid,
115547d523eSJim Harris 	    cmd->nsid, cmd->cdw10, cmd->cdw11);
116547d523eSJim Harris }
117547d523eSJim Harris 
118547d523eSJim Harris static void
119547d523eSJim Harris nvme_io_qpair_print_command(struct nvme_qpair *qpair,
120547d523eSJim Harris     struct nvme_command *cmd)
121547d523eSJim Harris {
122547d523eSJim Harris 
123547d523eSJim Harris 	switch (cmd->opc) {
124547d523eSJim Harris 	case NVME_OPC_WRITE:
125547d523eSJim Harris 	case NVME_OPC_READ:
126547d523eSJim Harris 	case NVME_OPC_WRITE_UNCORRECTABLE:
127547d523eSJim Harris 	case NVME_OPC_COMPARE:
128547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d "
129bdd1fd40SJim Harris 		    "lba:%llu len:%d\n",
130547d523eSJim Harris 		    get_io_opcode_string(cmd->opc), qpair->id, cmd->cid,
131bdd1fd40SJim Harris 		    cmd->nsid,
132bdd1fd40SJim Harris 		    ((unsigned long long)cmd->cdw11 << 32) + cmd->cdw10,
133547d523eSJim Harris 		    (cmd->cdw12 & 0xFFFF) + 1);
134547d523eSJim Harris 		break;
135547d523eSJim Harris 	case NVME_OPC_FLUSH:
136547d523eSJim Harris 	case NVME_OPC_DATASET_MANAGEMENT:
137547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s sqid:%d cid:%d nsid:%d\n",
138547d523eSJim Harris 		    get_io_opcode_string(cmd->opc), qpair->id, cmd->cid,
139547d523eSJim Harris 		    cmd->nsid);
140547d523eSJim Harris 		break;
141547d523eSJim Harris 	default:
142547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "%s (%02x) sqid:%d cid:%d nsid:%d\n",
143547d523eSJim Harris 		    get_io_opcode_string(cmd->opc), cmd->opc, qpair->id,
144547d523eSJim Harris 		    cmd->cid, cmd->nsid);
145547d523eSJim Harris 		break;
146547d523eSJim Harris 	}
147547d523eSJim Harris }
148547d523eSJim Harris 
149547d523eSJim Harris static void
150547d523eSJim Harris nvme_qpair_print_command(struct nvme_qpair *qpair, struct nvme_command *cmd)
151547d523eSJim Harris {
152547d523eSJim Harris 	if (qpair->id == 0)
153547d523eSJim Harris 		nvme_admin_qpair_print_command(qpair, cmd);
154547d523eSJim Harris 	else
155547d523eSJim Harris 		nvme_io_qpair_print_command(qpair, cmd);
156547d523eSJim Harris }
157547d523eSJim Harris 
158547d523eSJim Harris struct nvme_status_string {
159547d523eSJim Harris 
160547d523eSJim Harris 	uint16_t	sc;
161547d523eSJim Harris 	const char *	str;
162547d523eSJim Harris };
163547d523eSJim Harris 
164547d523eSJim Harris static struct nvme_status_string generic_status[] = {
165547d523eSJim Harris 	{ NVME_SC_SUCCESS, "SUCCESS" },
166547d523eSJim Harris 	{ NVME_SC_INVALID_OPCODE, "INVALID OPCODE" },
167547d523eSJim Harris 	{ NVME_SC_INVALID_FIELD, "INVALID_FIELD" },
168547d523eSJim Harris 	{ NVME_SC_COMMAND_ID_CONFLICT, "COMMAND ID CONFLICT" },
169547d523eSJim Harris 	{ NVME_SC_DATA_TRANSFER_ERROR, "DATA TRANSFER ERROR" },
170547d523eSJim Harris 	{ NVME_SC_ABORTED_POWER_LOSS, "ABORTED - POWER LOSS" },
171547d523eSJim Harris 	{ NVME_SC_INTERNAL_DEVICE_ERROR, "INTERNAL DEVICE ERROR" },
172547d523eSJim Harris 	{ NVME_SC_ABORTED_BY_REQUEST, "ABORTED - BY REQUEST" },
173547d523eSJim Harris 	{ NVME_SC_ABORTED_SQ_DELETION, "ABORTED - SQ DELETION" },
174547d523eSJim Harris 	{ NVME_SC_ABORTED_FAILED_FUSED, "ABORTED - FAILED FUSED" },
175547d523eSJim Harris 	{ NVME_SC_ABORTED_MISSING_FUSED, "ABORTED - MISSING FUSED" },
176547d523eSJim Harris 	{ NVME_SC_INVALID_NAMESPACE_OR_FORMAT, "INVALID NAMESPACE OR FORMAT" },
177547d523eSJim Harris 	{ NVME_SC_COMMAND_SEQUENCE_ERROR, "COMMAND SEQUENCE ERROR" },
178547d523eSJim Harris 	{ NVME_SC_LBA_OUT_OF_RANGE, "LBA OUT OF RANGE" },
179547d523eSJim Harris 	{ NVME_SC_CAPACITY_EXCEEDED, "CAPACITY EXCEEDED" },
180547d523eSJim Harris 	{ NVME_SC_NAMESPACE_NOT_READY, "NAMESPACE NOT READY" },
181547d523eSJim Harris 	{ 0xFFFF, "GENERIC" }
182547d523eSJim Harris };
183547d523eSJim Harris 
184547d523eSJim Harris static struct nvme_status_string command_specific_status[] = {
185547d523eSJim Harris 	{ NVME_SC_COMPLETION_QUEUE_INVALID, "INVALID COMPLETION QUEUE" },
186547d523eSJim Harris 	{ NVME_SC_INVALID_QUEUE_IDENTIFIER, "INVALID QUEUE IDENTIFIER" },
187547d523eSJim Harris 	{ NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED, "MAX QUEUE SIZE EXCEEDED" },
188547d523eSJim Harris 	{ NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED, "ABORT CMD LIMIT EXCEEDED" },
189547d523eSJim Harris 	{ NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED, "ASYNC LIMIT EXCEEDED" },
190547d523eSJim Harris 	{ NVME_SC_INVALID_FIRMWARE_SLOT, "INVALID FIRMWARE SLOT" },
191547d523eSJim Harris 	{ NVME_SC_INVALID_FIRMWARE_IMAGE, "INVALID FIRMWARE IMAGE" },
192547d523eSJim Harris 	{ NVME_SC_INVALID_INTERRUPT_VECTOR, "INVALID INTERRUPT VECTOR" },
193547d523eSJim Harris 	{ NVME_SC_INVALID_LOG_PAGE, "INVALID LOG PAGE" },
194547d523eSJim Harris 	{ NVME_SC_INVALID_FORMAT, "INVALID FORMAT" },
195547d523eSJim Harris 	{ NVME_SC_FIRMWARE_REQUIRES_RESET, "FIRMWARE REQUIRES RESET" },
196547d523eSJim Harris 	{ NVME_SC_CONFLICTING_ATTRIBUTES, "CONFLICTING ATTRIBUTES" },
197547d523eSJim Harris 	{ NVME_SC_INVALID_PROTECTION_INFO, "INVALID PROTECTION INFO" },
198547d523eSJim Harris 	{ NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE, "WRITE TO RO PAGE" },
199547d523eSJim Harris 	{ 0xFFFF, "COMMAND SPECIFIC" }
200547d523eSJim Harris };
201547d523eSJim Harris 
202547d523eSJim Harris static struct nvme_status_string media_error_status[] = {
203547d523eSJim Harris 	{ NVME_SC_WRITE_FAULTS, "WRITE FAULTS" },
204547d523eSJim Harris 	{ NVME_SC_UNRECOVERED_READ_ERROR, "UNRECOVERED READ ERROR" },
205547d523eSJim Harris 	{ NVME_SC_GUARD_CHECK_ERROR, "GUARD CHECK ERROR" },
206547d523eSJim Harris 	{ NVME_SC_APPLICATION_TAG_CHECK_ERROR, "APPLICATION TAG CHECK ERROR" },
207547d523eSJim Harris 	{ NVME_SC_REFERENCE_TAG_CHECK_ERROR, "REFERENCE TAG CHECK ERROR" },
208547d523eSJim Harris 	{ NVME_SC_COMPARE_FAILURE, "COMPARE FAILURE" },
209547d523eSJim Harris 	{ NVME_SC_ACCESS_DENIED, "ACCESS DENIED" },
210547d523eSJim Harris 	{ 0xFFFF, "MEDIA ERROR" }
211547d523eSJim Harris };
212547d523eSJim Harris 
213547d523eSJim Harris static const char *
214547d523eSJim Harris get_status_string(uint16_t sct, uint16_t sc)
215547d523eSJim Harris {
216547d523eSJim Harris 	struct nvme_status_string *entry;
217547d523eSJim Harris 
218547d523eSJim Harris 	switch (sct) {
219547d523eSJim Harris 	case NVME_SCT_GENERIC:
220547d523eSJim Harris 		entry = generic_status;
221547d523eSJim Harris 		break;
222547d523eSJim Harris 	case NVME_SCT_COMMAND_SPECIFIC:
223547d523eSJim Harris 		entry = command_specific_status;
224547d523eSJim Harris 		break;
225547d523eSJim Harris 	case NVME_SCT_MEDIA_ERROR:
226547d523eSJim Harris 		entry = media_error_status;
227547d523eSJim Harris 		break;
228547d523eSJim Harris 	case NVME_SCT_VENDOR_SPECIFIC:
229547d523eSJim Harris 		return ("VENDOR SPECIFIC");
230547d523eSJim Harris 	default:
231547d523eSJim Harris 		return ("RESERVED");
232547d523eSJim Harris 	}
233547d523eSJim Harris 
234547d523eSJim Harris 	while (entry->sc != 0xFFFF) {
235547d523eSJim Harris 		if (entry->sc == sc)
236547d523eSJim Harris 			return (entry->str);
237547d523eSJim Harris 		entry++;
238547d523eSJim Harris 	}
239547d523eSJim Harris 	return (entry->str);
240547d523eSJim Harris }
241547d523eSJim Harris 
242547d523eSJim Harris static void
243547d523eSJim Harris nvme_qpair_print_completion(struct nvme_qpair *qpair,
244547d523eSJim Harris     struct nvme_completion *cpl)
245547d523eSJim Harris {
246547d523eSJim Harris 	nvme_printf(qpair->ctrlr, "%s (%02x/%02x) sqid:%d cid:%d cdw0:%x\n",
247547d523eSJim Harris 	    get_status_string(cpl->status.sct, cpl->status.sc),
248547d523eSJim Harris 	    cpl->status.sct, cpl->status.sc, cpl->sqid, cpl->cid, cpl->cdw0);
249547d523eSJim Harris }
250547d523eSJim Harris 
251bb0ec6b3SJim Harris static boolean_t
2526cb06070SJim Harris nvme_completion_is_retry(const struct nvme_completion *cpl)
253bb0ec6b3SJim Harris {
254bb0ec6b3SJim Harris 	/*
255bb0ec6b3SJim Harris 	 * TODO: spec is not clear how commands that are aborted due
256bb0ec6b3SJim Harris 	 *  to TLER will be marked.  So for now, it seems
257bb0ec6b3SJim Harris 	 *  NAMESPACE_NOT_READY is the only case where we should
258bb0ec6b3SJim Harris 	 *  look at the DNR bit.
259bb0ec6b3SJim Harris 	 */
260cf81529cSJim Harris 	switch (cpl->status.sct) {
261bb0ec6b3SJim Harris 	case NVME_SCT_GENERIC:
262cf81529cSJim Harris 		switch (cpl->status.sc) {
263448195e7SJim Harris 		case NVME_SC_ABORTED_BY_REQUEST:
264bb0ec6b3SJim Harris 		case NVME_SC_NAMESPACE_NOT_READY:
265cf81529cSJim Harris 			if (cpl->status.dnr)
266bb0ec6b3SJim Harris 				return (0);
267bb0ec6b3SJim Harris 			else
268bb0ec6b3SJim Harris 				return (1);
269bb0ec6b3SJim Harris 		case NVME_SC_INVALID_OPCODE:
270bb0ec6b3SJim Harris 		case NVME_SC_INVALID_FIELD:
271bb0ec6b3SJim Harris 		case NVME_SC_COMMAND_ID_CONFLICT:
272bb0ec6b3SJim Harris 		case NVME_SC_DATA_TRANSFER_ERROR:
273bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_POWER_LOSS:
274bb0ec6b3SJim Harris 		case NVME_SC_INTERNAL_DEVICE_ERROR:
275bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_SQ_DELETION:
276bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_FAILED_FUSED:
277bb0ec6b3SJim Harris 		case NVME_SC_ABORTED_MISSING_FUSED:
278bb0ec6b3SJim Harris 		case NVME_SC_INVALID_NAMESPACE_OR_FORMAT:
279bb0ec6b3SJim Harris 		case NVME_SC_COMMAND_SEQUENCE_ERROR:
280bb0ec6b3SJim Harris 		case NVME_SC_LBA_OUT_OF_RANGE:
281bb0ec6b3SJim Harris 		case NVME_SC_CAPACITY_EXCEEDED:
282bb0ec6b3SJim Harris 		default:
283bb0ec6b3SJim Harris 			return (0);
284bb0ec6b3SJim Harris 		}
285bb0ec6b3SJim Harris 	case NVME_SCT_COMMAND_SPECIFIC:
286bb0ec6b3SJim Harris 	case NVME_SCT_MEDIA_ERROR:
287bb0ec6b3SJim Harris 	case NVME_SCT_VENDOR_SPECIFIC:
288bb0ec6b3SJim Harris 	default:
289bb0ec6b3SJim Harris 		return (0);
290bb0ec6b3SJim Harris 	}
291bb0ec6b3SJim Harris }
292bb0ec6b3SJim Harris 
29321b6da58SJim Harris static void
2946cb06070SJim Harris nvme_qpair_complete_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr,
2956cb06070SJim Harris     struct nvme_completion *cpl, boolean_t print_on_error)
296bb0ec6b3SJim Harris {
297ad697276SJim Harris 	struct nvme_request	*req;
298bb0ec6b3SJim Harris 	boolean_t		retry, error;
299bb0ec6b3SJim Harris 
300ad697276SJim Harris 	req = tr->req;
3016cb06070SJim Harris 	error = nvme_completion_is_error(cpl);
302cb5b7c13SJim Harris 	retry = error && nvme_completion_is_retry(cpl) &&
303cb5b7c13SJim Harris 	   req->retries < nvme_retry_count;
304ad697276SJim Harris 
3056cb06070SJim Harris 	if (error && print_on_error) {
306547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
307547d523eSJim Harris 		nvme_qpair_print_completion(qpair, cpl);
308bb0ec6b3SJim Harris 	}
309bb0ec6b3SJim Harris 
310bb0ec6b3SJim Harris 	qpair->act_tr[cpl->cid] = NULL;
311bb0ec6b3SJim Harris 
3126cb06070SJim Harris 	KASSERT(cpl->cid == req->cmd.cid, ("cpl cid does not match cmd cid\n"));
313bb0ec6b3SJim Harris 
314ad697276SJim Harris 	if (req->cb_fn && !retry)
315ad697276SJim Harris 		req->cb_fn(req->cb_arg, cpl);
316bb0ec6b3SJim Harris 
317bb0ec6b3SJim Harris 	mtx_lock(&qpair->lock);
318bb0ec6b3SJim Harris 	callout_stop(&tr->timer);
319bb0ec6b3SJim Harris 
320cb5b7c13SJim Harris 	if (retry) {
321cb5b7c13SJim Harris 		req->retries++;
322b846efd7SJim Harris 		nvme_qpair_submit_tracker(qpair, tr);
323cb5b7c13SJim Harris 	} else {
3241e526bc4SJim Harris 		if (req->type != NVME_REQUEST_NULL)
325a6e30963SJim Harris 			bus_dmamap_unload(qpair->dma_tag_payload,
326f2b19f67SJim Harris 			    tr->payload_dma_map);
327bb0ec6b3SJim Harris 
328ad697276SJim Harris 		nvme_free_request(req);
3290a0b08ccSJim Harris 		tr->req = NULL;
33021b6da58SJim Harris 
33165c2474eSJim Harris 		TAILQ_REMOVE(&qpair->outstanding_tr, tr, tailq);
33265c2474eSJim Harris 		TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
3330f71ecf7SJim Harris 
334f37c22a3SJim Harris 		/*
335f37c22a3SJim Harris 		 * If the controller is in the middle of resetting, don't
336f37c22a3SJim Harris 		 *  try to submit queued requests here - let the reset logic
337f37c22a3SJim Harris 		 *  handle that instead.
338f37c22a3SJim Harris 		 */
339f37c22a3SJim Harris 		if (!STAILQ_EMPTY(&qpair->queued_req) &&
340f37c22a3SJim Harris 		    !qpair->ctrlr->is_resetting) {
3410f71ecf7SJim Harris 			req = STAILQ_FIRST(&qpair->queued_req);
3420f71ecf7SJim Harris 			STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
343d6f54866SJim Harris 			_nvme_qpair_submit_request(qpair, req);
3440f71ecf7SJim Harris 		}
345c2e83b40SJim Harris 	}
346bb0ec6b3SJim Harris 
347bb0ec6b3SJim Harris 	mtx_unlock(&qpair->lock);
3486cb06070SJim Harris }
3496cb06070SJim Harris 
350b846efd7SJim Harris static void
351b846efd7SJim Harris nvme_qpair_manual_complete_tracker(struct nvme_qpair *qpair,
352232e2edbSJim Harris     struct nvme_tracker *tr, uint32_t sct, uint32_t sc, uint32_t dnr,
353b846efd7SJim Harris     boolean_t print_on_error)
354b846efd7SJim Harris {
355b846efd7SJim Harris 	struct nvme_completion	cpl;
356b846efd7SJim Harris 
357b846efd7SJim Harris 	memset(&cpl, 0, sizeof(cpl));
358b846efd7SJim Harris 	cpl.sqid = qpair->id;
359b846efd7SJim Harris 	cpl.cid = tr->cid;
360cf81529cSJim Harris 	cpl.status.sct = sct;
361cf81529cSJim Harris 	cpl.status.sc = sc;
362232e2edbSJim Harris 	cpl.status.dnr = dnr;
363b846efd7SJim Harris 	nvme_qpair_complete_tracker(qpair, tr, &cpl, print_on_error);
364b846efd7SJim Harris }
365b846efd7SJim Harris 
3666cb06070SJim Harris void
367232e2edbSJim Harris nvme_qpair_manual_complete_request(struct nvme_qpair *qpair,
368232e2edbSJim Harris     struct nvme_request *req, uint32_t sct, uint32_t sc,
369232e2edbSJim Harris     boolean_t print_on_error)
370232e2edbSJim Harris {
371232e2edbSJim Harris 	struct nvme_completion	cpl;
372232e2edbSJim Harris 	boolean_t		error;
373232e2edbSJim Harris 
374232e2edbSJim Harris 	memset(&cpl, 0, sizeof(cpl));
375232e2edbSJim Harris 	cpl.sqid = qpair->id;
376232e2edbSJim Harris 	cpl.status.sct = sct;
377232e2edbSJim Harris 	cpl.status.sc = sc;
378232e2edbSJim Harris 
379232e2edbSJim Harris 	error = nvme_completion_is_error(&cpl);
380232e2edbSJim Harris 
381232e2edbSJim Harris 	if (error && print_on_error) {
382547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
383547d523eSJim Harris 		nvme_qpair_print_completion(qpair, &cpl);
384232e2edbSJim Harris 	}
385232e2edbSJim Harris 
386232e2edbSJim Harris 	if (req->cb_fn)
387232e2edbSJim Harris 		req->cb_fn(req->cb_arg, &cpl);
388232e2edbSJim Harris 
389232e2edbSJim Harris 	nvme_free_request(req);
390232e2edbSJim Harris }
391232e2edbSJim Harris 
392232e2edbSJim Harris void
3936cb06070SJim Harris nvme_qpair_process_completions(struct nvme_qpair *qpair)
3946cb06070SJim Harris {
3956cb06070SJim Harris 	struct nvme_tracker	*tr;
3966cb06070SJim Harris 	struct nvme_completion	*cpl;
3976cb06070SJim Harris 
3986cb06070SJim Harris 	qpair->num_intr_handler_calls++;
3996cb06070SJim Harris 
400b846efd7SJim Harris 	if (!qpair->is_enabled)
401b846efd7SJim Harris 		/*
402b846efd7SJim Harris 		 * qpair is not enabled, likely because a controller reset is
403b846efd7SJim Harris 		 *  is in progress.  Ignore the interrupt - any I/O that was
404b846efd7SJim Harris 		 *  associated with this interrupt will get retried when the
405b846efd7SJim Harris 		 *  reset is complete.
406b846efd7SJim Harris 		 */
407b846efd7SJim Harris 		return;
408b846efd7SJim Harris 
4096cb06070SJim Harris 	while (1) {
4106cb06070SJim Harris 		cpl = &qpair->cpl[qpair->cq_head];
4116cb06070SJim Harris 
412cf81529cSJim Harris 		if (cpl->status.p != qpair->phase)
4136cb06070SJim Harris 			break;
4146cb06070SJim Harris 
4156cb06070SJim Harris 		tr = qpair->act_tr[cpl->cid];
4166cb06070SJim Harris 
4176cb06070SJim Harris 		if (tr != NULL) {
4186cb06070SJim Harris 			nvme_qpair_complete_tracker(qpair, tr, cpl, TRUE);
4196cb06070SJim Harris 			qpair->sq_head = cpl->sqhd;
4206cb06070SJim Harris 		} else {
421547d523eSJim Harris 			nvme_printf(qpair->ctrlr,
422547d523eSJim Harris 			    "cpl does not map to outstanding cmd\n");
4236cb06070SJim Harris 			nvme_dump_completion(cpl);
4246cb06070SJim Harris 			KASSERT(0, ("received completion for unknown cmd\n"));
4256cb06070SJim Harris 		}
426bb0ec6b3SJim Harris 
427bb0ec6b3SJim Harris 		if (++qpair->cq_head == qpair->num_entries) {
428bb0ec6b3SJim Harris 			qpair->cq_head = 0;
429bb0ec6b3SJim Harris 			qpair->phase = !qpair->phase;
430bb0ec6b3SJim Harris 		}
431bb0ec6b3SJim Harris 
432bb0ec6b3SJim Harris 		nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].cq_hdbl,
433bb0ec6b3SJim Harris 		    qpair->cq_head);
434bb0ec6b3SJim Harris 	}
435bb0ec6b3SJim Harris }
436bb0ec6b3SJim Harris 
437bb0ec6b3SJim Harris static void
438bb0ec6b3SJim Harris nvme_qpair_msix_handler(void *arg)
439bb0ec6b3SJim Harris {
440bb0ec6b3SJim Harris 	struct nvme_qpair *qpair = arg;
441bb0ec6b3SJim Harris 
442bb0ec6b3SJim Harris 	nvme_qpair_process_completions(qpair);
443bb0ec6b3SJim Harris }
444bb0ec6b3SJim Harris 
445a965389bSScott Long int
446bb0ec6b3SJim Harris nvme_qpair_construct(struct nvme_qpair *qpair, uint32_t id,
44721b6da58SJim Harris     uint16_t vector, uint32_t num_entries, uint32_t num_trackers,
4488d09e3c4SJim Harris     struct nvme_controller *ctrlr)
449bb0ec6b3SJim Harris {
45021b6da58SJim Harris 	struct nvme_tracker	*tr;
451a965389bSScott Long 	size_t			cmdsz, cplsz, prpsz, allocsz, prpmemsz;
452a965389bSScott Long 	uint64_t		queuemem_phys, prpmem_phys, list_phys;
453a965389bSScott Long 	uint8_t			*queuemem, *prpmem, *prp_list;
454a965389bSScott Long 	int			i, err;
455bb0ec6b3SJim Harris 
456bb0ec6b3SJim Harris 	qpair->id = id;
457bb0ec6b3SJim Harris 	qpair->vector = vector;
458bb0ec6b3SJim Harris 	qpair->num_entries = num_entries;
4590f71ecf7SJim Harris 	qpair->num_trackers = num_trackers;
460bb0ec6b3SJim Harris 	qpair->ctrlr = ctrlr;
461bb0ec6b3SJim Harris 
462bb0ec6b3SJim Harris 	if (ctrlr->msix_enabled) {
463bb0ec6b3SJim Harris 
464bb0ec6b3SJim Harris 		/*
465bb0ec6b3SJim Harris 		 * MSI-X vector resource IDs start at 1, so we add one to
466bb0ec6b3SJim Harris 		 *  the queue's vector to get the corresponding rid to use.
467bb0ec6b3SJim Harris 		 */
468bb0ec6b3SJim Harris 		qpair->rid = vector + 1;
469bb0ec6b3SJim Harris 
470e5af5854SJim Harris 		qpair->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ,
471e5af5854SJim Harris 		    &qpair->rid, RF_ACTIVE);
472bb0ec6b3SJim Harris 		bus_setup_intr(ctrlr->dev, qpair->res,
473bb0ec6b3SJim Harris 		    INTR_TYPE_MISC | INTR_MPSAFE, NULL,
474bb0ec6b3SJim Harris 		    nvme_qpair_msix_handler, qpair, &qpair->tag);
475bb0ec6b3SJim Harris 	}
476bb0ec6b3SJim Harris 
477bb0ec6b3SJim Harris 	mtx_init(&qpair->lock, "nvme qpair lock", NULL, MTX_DEF);
478bb0ec6b3SJim Harris 
4791416ef36SJim Harris 	/* Note: NVMe PRP format is restricted to 4-byte alignment. */
480a6e30963SJim Harris 	err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
4811416ef36SJim Harris 	    4, PAGE_SIZE, BUS_SPACE_MAXADDR,
4828d09e3c4SJim Harris 	    BUS_SPACE_MAXADDR, NULL, NULL, NVME_MAX_XFER_SIZE,
4838d09e3c4SJim Harris 	    (NVME_MAX_XFER_SIZE/PAGE_SIZE)+1, PAGE_SIZE, 0,
484a6e30963SJim Harris 	    NULL, NULL, &qpair->dma_tag_payload);
485a965389bSScott Long 	if (err != 0) {
486a6e30963SJim Harris 		nvme_printf(ctrlr, "payload tag create failed %d\n", err);
487a965389bSScott Long 		goto out;
488a965389bSScott Long 	}
489a965389bSScott Long 
490a965389bSScott Long 	/*
491a965389bSScott Long 	 * Each component must be page aligned, and individual PRP lists
492a965389bSScott Long 	 * cannot cross a page boundary.
493a965389bSScott Long 	 */
494a965389bSScott Long 	cmdsz = qpair->num_entries * sizeof(struct nvme_command);
495a965389bSScott Long 	cmdsz = roundup2(cmdsz, PAGE_SIZE);
496a965389bSScott Long 	cplsz = qpair->num_entries * sizeof(struct nvme_completion);
497a965389bSScott Long 	cplsz = roundup2(cplsz, PAGE_SIZE);
498a965389bSScott Long 	prpsz = sizeof(uint64_t) * NVME_MAX_PRP_LIST_ENTRIES;;
499a965389bSScott Long 	prpmemsz = qpair->num_trackers * prpsz;
500a965389bSScott Long 	allocsz = cmdsz + cplsz + prpmemsz;
501a6e30963SJim Harris 
502a6e30963SJim Harris 	err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev),
503a965389bSScott Long 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
504a965389bSScott Long 	    allocsz, 1, allocsz, 0, NULL, NULL, &qpair->dma_tag);
505a965389bSScott Long 	if (err != 0) {
506a6e30963SJim Harris 		nvme_printf(ctrlr, "tag create failed %d\n", err);
507a965389bSScott Long 		goto out;
508a965389bSScott Long 	}
509a965389bSScott Long 
510a965389bSScott Long 	if (bus_dmamem_alloc(qpair->dma_tag, (void **)&queuemem,
511a965389bSScott Long 	    BUS_DMA_NOWAIT, &qpair->queuemem_map)) {
512a965389bSScott Long 		nvme_printf(ctrlr, "failed to alloc qpair memory\n");
513a965389bSScott Long 		goto out;
514a965389bSScott Long 	}
515a965389bSScott Long 
516a965389bSScott Long 	if (bus_dmamap_load(qpair->dma_tag, qpair->queuemem_map,
517a965389bSScott Long 	    queuemem, allocsz, nvme_single_map, &queuemem_phys, 0) != 0) {
518a965389bSScott Long 		nvme_printf(ctrlr, "failed to load qpair memory\n");
519a965389bSScott Long 		goto out;
520a965389bSScott Long 	}
521bb0ec6b3SJim Harris 
522bb0ec6b3SJim Harris 	qpair->num_cmds = 0;
5236568ebfcSJim Harris 	qpair->num_intr_handler_calls = 0;
524a965389bSScott Long 	qpair->cmd = (struct nvme_command *)queuemem;
525a965389bSScott Long 	qpair->cpl = (struct nvme_completion *)(queuemem + cmdsz);
526a965389bSScott Long 	prpmem = (uint8_t *)(queuemem + cmdsz + cplsz);
527a965389bSScott Long 	qpair->cmd_bus_addr = queuemem_phys;
528a965389bSScott Long 	qpair->cpl_bus_addr = queuemem_phys + cmdsz;
529a965389bSScott Long 	prpmem_phys = queuemem_phys + cmdsz + cplsz;
530bb0ec6b3SJim Harris 
531bb0ec6b3SJim Harris 	qpair->sq_tdbl_off = nvme_mmio_offsetof(doorbell[id].sq_tdbl);
532bb0ec6b3SJim Harris 	qpair->cq_hdbl_off = nvme_mmio_offsetof(doorbell[id].cq_hdbl);
533bb0ec6b3SJim Harris 
53465c2474eSJim Harris 	TAILQ_INIT(&qpair->free_tr);
53565c2474eSJim Harris 	TAILQ_INIT(&qpair->outstanding_tr);
5360f71ecf7SJim Harris 	STAILQ_INIT(&qpair->queued_req);
537bb0ec6b3SJim Harris 
538a965389bSScott Long 	list_phys = prpmem_phys;
539a965389bSScott Long 	prp_list = prpmem;
5400f71ecf7SJim Harris 	for (i = 0; i < qpair->num_trackers; i++) {
541a965389bSScott Long 
542a965389bSScott Long 		if (list_phys + prpsz > prpmem_phys + prpmemsz) {
543a965389bSScott Long 			qpair->num_trackers = i;
544a965389bSScott Long 			break;
54521b6da58SJim Harris 		}
54621b6da58SJim Harris 
547a965389bSScott Long 		/*
548a965389bSScott Long 		 * Make sure that the PRP list for this tracker doesn't
549a965389bSScott Long 		 * overflow to another page.
550a965389bSScott Long 		 */
551a965389bSScott Long 		if (trunc_page(list_phys) !=
552a965389bSScott Long 		    trunc_page(list_phys + prpsz - 1)) {
553a965389bSScott Long 			list_phys = roundup2(list_phys, PAGE_SIZE);
554a965389bSScott Long 			prp_list =
555a965389bSScott Long 			    (uint8_t *)roundup2((uintptr_t)prp_list, PAGE_SIZE);
556a965389bSScott Long 		}
557a965389bSScott Long 
558a965389bSScott Long 		tr = malloc(sizeof(*tr), M_NVME, M_ZERO | M_WAITOK);
559a965389bSScott Long 		bus_dmamap_create(qpair->dma_tag_payload, 0,
560a965389bSScott Long 		    &tr->payload_dma_map);
561a965389bSScott Long 		callout_init(&tr->timer, 1);
562a965389bSScott Long 		tr->cid = i;
563a965389bSScott Long 		tr->qpair = qpair;
564a965389bSScott Long 		tr->prp = (uint64_t *)prp_list;
565a965389bSScott Long 		tr->prp_bus_addr = list_phys;
566a965389bSScott Long 		TAILQ_INSERT_HEAD(&qpair->free_tr, tr, tailq);
567a965389bSScott Long 		list_phys += prpsz;
568a965389bSScott Long 		prp_list += prpsz;
569a965389bSScott Long 	}
570a965389bSScott Long 
571a965389bSScott Long 	if (qpair->num_trackers == 0) {
572a965389bSScott Long 		nvme_printf(ctrlr, "failed to allocate enough trackers\n");
573a965389bSScott Long 		goto out;
574a965389bSScott Long 	}
575a965389bSScott Long 
576a965389bSScott Long 	qpair->act_tr = malloc(sizeof(struct nvme_tracker *) *
577a965389bSScott Long 	    qpair->num_entries, M_NVME, M_ZERO | M_WAITOK);
578a965389bSScott Long 	return (0);
579a965389bSScott Long 
580a965389bSScott Long out:
581a965389bSScott Long 	nvme_qpair_destroy(qpair);
582a965389bSScott Long 	return (ENOMEM);
583bb0ec6b3SJim Harris }
584bb0ec6b3SJim Harris 
585bb0ec6b3SJim Harris static void
586bb0ec6b3SJim Harris nvme_qpair_destroy(struct nvme_qpair *qpair)
587bb0ec6b3SJim Harris {
588bb0ec6b3SJim Harris 	struct nvme_tracker	*tr;
589bb0ec6b3SJim Harris 
590bb0ec6b3SJim Harris 	if (qpair->tag)
591bb0ec6b3SJim Harris 		bus_teardown_intr(qpair->ctrlr->dev, qpair->res, qpair->tag);
592bb0ec6b3SJim Harris 
593a965389bSScott Long 	if (mtx_initialized(&qpair->lock))
594a965389bSScott Long 		mtx_destroy(&qpair->lock);
595a965389bSScott Long 
596bb0ec6b3SJim Harris 	if (qpair->res)
597bb0ec6b3SJim Harris 		bus_release_resource(qpair->ctrlr->dev, SYS_RES_IRQ,
598bb0ec6b3SJim Harris 		    rman_get_rid(qpair->res), qpair->res);
599bb0ec6b3SJim Harris 
600a965389bSScott Long 	if (qpair->cmd != NULL) {
601a965389bSScott Long 		bus_dmamap_unload(qpair->dma_tag, qpair->queuemem_map);
602a965389bSScott Long 		bus_dmamem_free(qpair->dma_tag, qpair->cmd,
603a965389bSScott Long 		    qpair->queuemem_map);
6043d7eb41cSJim Harris 	}
6053d7eb41cSJim Harris 
606bb0ec6b3SJim Harris 	if (qpair->dma_tag)
607bb0ec6b3SJim Harris 		bus_dma_tag_destroy(qpair->dma_tag);
608bb0ec6b3SJim Harris 
609a6e30963SJim Harris 	if (qpair->dma_tag_payload)
610a6e30963SJim Harris 		bus_dma_tag_destroy(qpair->dma_tag_payload);
611a6e30963SJim Harris 
612bb0ec6b3SJim Harris 	if (qpair->act_tr)
613bb0ec6b3SJim Harris 		free(qpair->act_tr, M_NVME);
614bb0ec6b3SJim Harris 
61565c2474eSJim Harris 	while (!TAILQ_EMPTY(&qpair->free_tr)) {
61665c2474eSJim Harris 		tr = TAILQ_FIRST(&qpair->free_tr);
61765c2474eSJim Harris 		TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
618f2b19f67SJim Harris 		bus_dmamap_destroy(qpair->dma_tag, tr->payload_dma_map);
619bb0ec6b3SJim Harris 		free(tr, M_NVME);
620bb0ec6b3SJim Harris 	}
621bb0ec6b3SJim Harris }
622bb0ec6b3SJim Harris 
623b846efd7SJim Harris static void
624b846efd7SJim Harris nvme_admin_qpair_abort_aers(struct nvme_qpair *qpair)
625b846efd7SJim Harris {
626b846efd7SJim Harris 	struct nvme_tracker	*tr;
627b846efd7SJim Harris 
628b846efd7SJim Harris 	tr = TAILQ_FIRST(&qpair->outstanding_tr);
629b846efd7SJim Harris 	while (tr != NULL) {
630b846efd7SJim Harris 		if (tr->req->cmd.opc == NVME_OPC_ASYNC_EVENT_REQUEST) {
631b846efd7SJim Harris 			nvme_qpair_manual_complete_tracker(qpair, tr,
632232e2edbSJim Harris 			    NVME_SCT_GENERIC, NVME_SC_ABORTED_SQ_DELETION, 0,
633b846efd7SJim Harris 			    FALSE);
634b846efd7SJim Harris 			tr = TAILQ_FIRST(&qpair->outstanding_tr);
635b846efd7SJim Harris 		} else {
636b846efd7SJim Harris 			tr = TAILQ_NEXT(tr, tailq);
637b846efd7SJim Harris 		}
638b846efd7SJim Harris 	}
639b846efd7SJim Harris }
640b846efd7SJim Harris 
641bb0ec6b3SJim Harris void
642bb0ec6b3SJim Harris nvme_admin_qpair_destroy(struct nvme_qpair *qpair)
643bb0ec6b3SJim Harris {
644bb0ec6b3SJim Harris 
645b846efd7SJim Harris 	nvme_admin_qpair_abort_aers(qpair);
646bb0ec6b3SJim Harris 	nvme_qpair_destroy(qpair);
647bb0ec6b3SJim Harris }
648bb0ec6b3SJim Harris 
649bb0ec6b3SJim Harris void
650bb0ec6b3SJim Harris nvme_io_qpair_destroy(struct nvme_qpair *qpair)
651bb0ec6b3SJim Harris {
652bb0ec6b3SJim Harris 
653bb0ec6b3SJim Harris 	nvme_qpair_destroy(qpair);
654bb0ec6b3SJim Harris }
655bb0ec6b3SJim Harris 
656bb0ec6b3SJim Harris static void
6570a0b08ccSJim Harris nvme_abort_complete(void *arg, const struct nvme_completion *status)
6580a0b08ccSJim Harris {
659879de699SJim Harris 	struct nvme_tracker	*tr = arg;
660879de699SJim Harris 
661879de699SJim Harris 	/*
662879de699SJim Harris 	 * If cdw0 == 1, the controller was not able to abort the command
663879de699SJim Harris 	 *  we requested.  We still need to check the active tracker array,
664879de699SJim Harris 	 *  to cover race where I/O timed out at same time controller was
665879de699SJim Harris 	 *  completing the I/O.
666879de699SJim Harris 	 */
667879de699SJim Harris 	if (status->cdw0 == 1 && tr->qpair->act_tr[tr->cid] != NULL) {
668879de699SJim Harris 		/*
669879de699SJim Harris 		 * An I/O has timed out, and the controller was unable to
670879de699SJim Harris 		 *  abort it for some reason.  Construct a fake completion
671879de699SJim Harris 		 *  status, and then complete the I/O's tracker manually.
672879de699SJim Harris 		 */
673547d523eSJim Harris 		nvme_printf(tr->qpair->ctrlr,
674547d523eSJim Harris 		    "abort command failed, aborting command manually\n");
675b846efd7SJim Harris 		nvme_qpair_manual_complete_tracker(tr->qpair, tr,
676232e2edbSJim Harris 		    NVME_SCT_GENERIC, NVME_SC_ABORTED_BY_REQUEST, 0, TRUE);
677879de699SJim Harris 	}
678879de699SJim Harris }
679879de699SJim Harris 
680879de699SJim Harris static void
681bb0ec6b3SJim Harris nvme_timeout(void *arg)
682bb0ec6b3SJim Harris {
683448195e7SJim Harris 	struct nvme_tracker	*tr = arg;
68412d191ecSJim Harris 	struct nvme_qpair	*qpair = tr->qpair;
68512d191ecSJim Harris 	struct nvme_controller	*ctrlr = qpair->ctrlr;
68612d191ecSJim Harris 	union csts_register	csts;
687448195e7SJim Harris 
68848ce3178SJim Harris 	/* Read csts to get value of cfs - controller fatal status. */
68912d191ecSJim Harris 	csts.raw = nvme_mmio_read_4(ctrlr, csts);
69012d191ecSJim Harris 
69148ce3178SJim Harris 	if (ctrlr->enable_aborts && csts.bits.cfs == 0) {
69248ce3178SJim Harris 		/*
69348ce3178SJim Harris 		 * If aborts are enabled, only use them if the controller is
69448ce3178SJim Harris 		 *  not reporting fatal status.
69548ce3178SJim Harris 		 */
69612d191ecSJim Harris 		nvme_ctrlr_cmd_abort(ctrlr, tr->cid, qpair->id,
697879de699SJim Harris 		    nvme_abort_complete, tr);
69848ce3178SJim Harris 	} else
69948ce3178SJim Harris 		nvme_ctrlr_reset(ctrlr);
700bb0ec6b3SJim Harris }
701bb0ec6b3SJim Harris 
702bb0ec6b3SJim Harris void
703b846efd7SJim Harris nvme_qpair_submit_tracker(struct nvme_qpair *qpair, struct nvme_tracker *tr)
704bb0ec6b3SJim Harris {
705ad697276SJim Harris 	struct nvme_request	*req;
70694143332SJim Harris 	struct nvme_controller	*ctrlr;
707bb0ec6b3SJim Harris 
708b846efd7SJim Harris 	mtx_assert(&qpair->lock, MA_OWNED);
709b846efd7SJim Harris 
710ad697276SJim Harris 	req = tr->req;
711ad697276SJim Harris 	req->cmd.cid = tr->cid;
712bb0ec6b3SJim Harris 	qpair->act_tr[tr->cid] = tr;
71394143332SJim Harris 	ctrlr = qpair->ctrlr;
714bb0ec6b3SJim Harris 
71594143332SJim Harris 	if (req->timeout)
716633c5729SJim Harris #if __FreeBSD_version >= 800030
71794143332SJim Harris 		callout_reset_curcpu(&tr->timer, ctrlr->timeout_period * hz,
718633c5729SJim Harris 		    nvme_timeout, tr);
719633c5729SJim Harris #else
72094143332SJim Harris 		callout_reset(&tr->timer, ctrlr->timeout_period * hz,
72194143332SJim Harris 		    nvme_timeout, tr);
722633c5729SJim Harris #endif
723bb0ec6b3SJim Harris 
724bb0ec6b3SJim Harris 	/* Copy the command from the tracker to the submission queue. */
725ad697276SJim Harris 	memcpy(&qpair->cmd[qpair->sq_tail], &req->cmd, sizeof(req->cmd));
726bb0ec6b3SJim Harris 
727bb0ec6b3SJim Harris 	if (++qpair->sq_tail == qpair->num_entries)
728bb0ec6b3SJim Harris 		qpair->sq_tail = 0;
729bb0ec6b3SJim Harris 
730bb0ec6b3SJim Harris 	wmb();
731bb0ec6b3SJim Harris 	nvme_mmio_write_4(qpair->ctrlr, doorbell[qpair->id].sq_tdbl,
732bb0ec6b3SJim Harris 	    qpair->sq_tail);
733bb0ec6b3SJim Harris 
734bb0ec6b3SJim Harris 	qpair->num_cmds++;
735bb0ec6b3SJim Harris }
7365ae9ed68SJim Harris 
737d6f54866SJim Harris static void
738ca269f32SJim Harris nvme_payload_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
739ca269f32SJim Harris {
740ca269f32SJim Harris 	struct nvme_tracker 	*tr = arg;
741ca269f32SJim Harris 	uint32_t		cur_nseg;
742ca269f32SJim Harris 
743ca269f32SJim Harris 	/*
744ca269f32SJim Harris 	 * If the mapping operation failed, return immediately.  The caller
745ca269f32SJim Harris 	 *  is responsible for detecting the error status and failing the
746ca269f32SJim Harris 	 *  tracker manually.
747ca269f32SJim Harris 	 */
748a6e30963SJim Harris 	if (error != 0) {
749a6e30963SJim Harris 		nvme_printf(tr->qpair->ctrlr,
750a6e30963SJim Harris 		    "nvme_payload_map err %d\n", error);
751ca269f32SJim Harris 		return;
752a6e30963SJim Harris 	}
753ca269f32SJim Harris 
754ca269f32SJim Harris 	/*
755ca269f32SJim Harris 	 * Note that we specified PAGE_SIZE for alignment and max
756ca269f32SJim Harris 	 *  segment size when creating the bus dma tags.  So here
757ca269f32SJim Harris 	 *  we can safely just transfer each segment to its
758ca269f32SJim Harris 	 *  associated PRP entry.
759ca269f32SJim Harris 	 */
760ca269f32SJim Harris 	tr->req->cmd.prp1 = seg[0].ds_addr;
761ca269f32SJim Harris 
762ca269f32SJim Harris 	if (nseg == 2) {
763ca269f32SJim Harris 		tr->req->cmd.prp2 = seg[1].ds_addr;
764ca269f32SJim Harris 	} else if (nseg > 2) {
765ca269f32SJim Harris 		cur_nseg = 1;
766ca269f32SJim Harris 		tr->req->cmd.prp2 = (uint64_t)tr->prp_bus_addr;
767ca269f32SJim Harris 		while (cur_nseg < nseg) {
768ca269f32SJim Harris 			tr->prp[cur_nseg-1] =
769ca269f32SJim Harris 			    (uint64_t)seg[cur_nseg].ds_addr;
770ca269f32SJim Harris 			cur_nseg++;
771ca269f32SJim Harris 		}
772a6e30963SJim Harris 	} else {
773a6e30963SJim Harris 		/*
774a6e30963SJim Harris 		 * prp2 should not be used by the controller
775a6e30963SJim Harris 		 *  since there is only one segment, but set
776a6e30963SJim Harris 		 *  to 0 just to be safe.
777a6e30963SJim Harris 		 */
778a6e30963SJim Harris 		tr->req->cmd.prp2 = 0;
779ca269f32SJim Harris 	}
780ca269f32SJim Harris 
781ca269f32SJim Harris 	nvme_qpair_submit_tracker(tr->qpair, tr);
782ca269f32SJim Harris }
783ca269f32SJim Harris 
784ca269f32SJim Harris static void
785d6f54866SJim Harris _nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
7865ae9ed68SJim Harris {
7875ae9ed68SJim Harris 	struct nvme_tracker	*tr;
788e2b99004SJim Harris 	int			err = 0;
7895ae9ed68SJim Harris 
790d6f54866SJim Harris 	mtx_assert(&qpair->lock, MA_OWNED);
7915ae9ed68SJim Harris 
79265c2474eSJim Harris 	tr = TAILQ_FIRST(&qpair->free_tr);
793232e2edbSJim Harris 	req->qpair = qpair;
79421b6da58SJim Harris 
795b846efd7SJim Harris 	if (tr == NULL || !qpair->is_enabled) {
7960f71ecf7SJim Harris 		/*
797b846efd7SJim Harris 		 * No tracker is available, or the qpair is disabled due to
798232e2edbSJim Harris 		 *  an in-progress controller-level reset or controller
799232e2edbSJim Harris 		 *  failure.
800232e2edbSJim Harris 		 */
801232e2edbSJim Harris 
802232e2edbSJim Harris 		if (qpair->ctrlr->is_failed) {
803232e2edbSJim Harris 			/*
804232e2edbSJim Harris 			 * The controller has failed.  Post the request to a
805232e2edbSJim Harris 			 *  task where it will be aborted, so that we do not
806232e2edbSJim Harris 			 *  invoke the request's callback in the context
807232e2edbSJim Harris 			 *  of the submission.
808232e2edbSJim Harris 			 */
809232e2edbSJim Harris 			nvme_ctrlr_post_failed_request(qpair->ctrlr, req);
810232e2edbSJim Harris 		} else {
811232e2edbSJim Harris 			/*
812232e2edbSJim Harris 			 * Put the request on the qpair's request queue to be
813232e2edbSJim Harris 			 *  processed when a tracker frees up via a command
814232e2edbSJim Harris 			 *  completion or when the controller reset is
815232e2edbSJim Harris 			 *  completed.
8160f71ecf7SJim Harris 			 */
8170f71ecf7SJim Harris 			STAILQ_INSERT_TAIL(&qpair->queued_req, req, stailq);
818232e2edbSJim Harris 		}
819d6f54866SJim Harris 		return;
82021b6da58SJim Harris 	}
82121b6da58SJim Harris 
82265c2474eSJim Harris 	TAILQ_REMOVE(&qpair->free_tr, tr, tailq);
82365c2474eSJim Harris 	TAILQ_INSERT_TAIL(&qpair->outstanding_tr, tr, tailq);
8245ae9ed68SJim Harris 	tr->req = req;
8255ae9ed68SJim Harris 
8261e526bc4SJim Harris 	switch (req->type) {
8271e526bc4SJim Harris 	case NVME_REQUEST_VADDR:
8287b68ae1eSJim Harris 		KASSERT(req->payload_size <= qpair->ctrlr->max_xfer_size,
8297b68ae1eSJim Harris 		    ("payload_size (%d) exceeds max_xfer_size (%d)\n",
8307b68ae1eSJim Harris 		    req->payload_size, qpair->ctrlr->max_xfer_size));
831a6e30963SJim Harris 		err = bus_dmamap_load(tr->qpair->dma_tag_payload,
832a6e30963SJim Harris 		    tr->payload_dma_map, req->u.payload, req->payload_size,
833a6e30963SJim Harris 		    nvme_payload_map, tr, 0);
8345ae9ed68SJim Harris 		if (err != 0)
835e2b99004SJim Harris 			nvme_printf(qpair->ctrlr,
836e2b99004SJim Harris 			    "bus_dmamap_load returned 0x%x!\n", err);
8371e526bc4SJim Harris 		break;
8381e526bc4SJim Harris 	case NVME_REQUEST_NULL:
839b846efd7SJim Harris 		nvme_qpair_submit_tracker(tr->qpair, tr);
8401e526bc4SJim Harris 		break;
8415fdf9c3cSJim Harris #ifdef NVME_UNMAPPED_BIO_SUPPORT
8425fdf9c3cSJim Harris 	case NVME_REQUEST_BIO:
8437b68ae1eSJim Harris 		KASSERT(req->u.bio->bio_bcount <= qpair->ctrlr->max_xfer_size,
8447b68ae1eSJim Harris 		    ("bio->bio_bcount (%jd) exceeds max_xfer_size (%d)\n",
8457b68ae1eSJim Harris 		    (intmax_t)req->u.bio->bio_bcount,
8467b68ae1eSJim Harris 		    qpair->ctrlr->max_xfer_size));
847a6e30963SJim Harris 		err = bus_dmamap_load_bio(tr->qpair->dma_tag_payload,
8485fdf9c3cSJim Harris 		    tr->payload_dma_map, req->u.bio, nvme_payload_map, tr, 0);
8495fdf9c3cSJim Harris 		if (err != 0)
850e2b99004SJim Harris 			nvme_printf(qpair->ctrlr,
851e2b99004SJim Harris 			    "bus_dmamap_load_bio returned 0x%x!\n", err);
8525fdf9c3cSJim Harris 		break;
8535fdf9c3cSJim Harris #endif
8541e526bc4SJim Harris 	default:
8551e526bc4SJim Harris 		panic("unknown nvme request type 0x%x\n", req->type);
8561e526bc4SJim Harris 		break;
8575ae9ed68SJim Harris 	}
858e2b99004SJim Harris 
859e2b99004SJim Harris 	if (err != 0) {
860e2b99004SJim Harris 		/*
861e2b99004SJim Harris 		 * The dmamap operation failed, so we manually fail the
862e2b99004SJim Harris 		 *  tracker here with DATA_TRANSFER_ERROR status.
863e2b99004SJim Harris 		 *
864e2b99004SJim Harris 		 * nvme_qpair_manual_complete_tracker must not be called
865e2b99004SJim Harris 		 *  with the qpair lock held.
866e2b99004SJim Harris 		 */
867e2b99004SJim Harris 		mtx_unlock(&qpair->lock);
868e2b99004SJim Harris 		nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
869e2b99004SJim Harris 		    NVME_SC_DATA_TRANSFER_ERROR, 1 /* do not retry */, TRUE);
870e2b99004SJim Harris 		mtx_lock(&qpair->lock);
871e2b99004SJim Harris 	}
872d6f54866SJim Harris }
8735ae9ed68SJim Harris 
874d6f54866SJim Harris void
875d6f54866SJim Harris nvme_qpair_submit_request(struct nvme_qpair *qpair, struct nvme_request *req)
876d6f54866SJim Harris {
877d6f54866SJim Harris 
878d6f54866SJim Harris 	mtx_lock(&qpair->lock);
879d6f54866SJim Harris 	_nvme_qpair_submit_request(qpair, req);
8805ae9ed68SJim Harris 	mtx_unlock(&qpair->lock);
8815ae9ed68SJim Harris }
882b846efd7SJim Harris 
883b846efd7SJim Harris static void
884b846efd7SJim Harris nvme_qpair_enable(struct nvme_qpair *qpair)
885b846efd7SJim Harris {
886b846efd7SJim Harris 
887b846efd7SJim Harris 	qpair->is_enabled = TRUE;
888cb5b7c13SJim Harris }
889cb5b7c13SJim Harris 
890cb5b7c13SJim Harris void
891cb5b7c13SJim Harris nvme_qpair_reset(struct nvme_qpair *qpair)
892cb5b7c13SJim Harris {
893cb5b7c13SJim Harris 
894b846efd7SJim Harris 	qpair->sq_head = qpair->sq_tail = qpair->cq_head = 0;
895b846efd7SJim Harris 
896b846efd7SJim Harris 	/*
897b846efd7SJim Harris 	 * First time through the completion queue, HW will set phase
898b846efd7SJim Harris 	 *  bit on completions to 1.  So set this to 1 here, indicating
899b846efd7SJim Harris 	 *  we're looking for a 1 to know which entries have completed.
900b846efd7SJim Harris 	 *  we'll toggle the bit each time when the completion queue
901b846efd7SJim Harris 	 *  rolls over.
902b846efd7SJim Harris 	 */
903b846efd7SJim Harris 	qpair->phase = 1;
904b846efd7SJim Harris 
905b846efd7SJim Harris 	memset(qpair->cmd, 0,
906b846efd7SJim Harris 	    qpair->num_entries * sizeof(struct nvme_command));
907b846efd7SJim Harris 	memset(qpair->cpl, 0,
908b846efd7SJim Harris 	    qpair->num_entries * sizeof(struct nvme_completion));
909b846efd7SJim Harris }
910b846efd7SJim Harris 
911b846efd7SJim Harris void
912b846efd7SJim Harris nvme_admin_qpair_enable(struct nvme_qpair *qpair)
913b846efd7SJim Harris {
91443a37256SJim Harris 	struct nvme_tracker		*tr;
91543a37256SJim Harris 	struct nvme_tracker		*tr_temp;
91643a37256SJim Harris 
91743a37256SJim Harris 	/*
91843a37256SJim Harris 	 * Manually abort each outstanding admin command.  Do not retry
91943a37256SJim Harris 	 *  admin commands found here, since they will be left over from
92043a37256SJim Harris 	 *  a controller reset and its likely the context in which the
92143a37256SJim Harris 	 *  command was issued no longer applies.
92243a37256SJim Harris 	 */
92343a37256SJim Harris 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
924547d523eSJim Harris 		nvme_printf(qpair->ctrlr,
92543a37256SJim Harris 		    "aborting outstanding admin command\n");
92643a37256SJim Harris 		nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
92743a37256SJim Harris 		    NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE);
92843a37256SJim Harris 	}
929b846efd7SJim Harris 
930b846efd7SJim Harris 	nvme_qpair_enable(qpair);
931b846efd7SJim Harris }
932b846efd7SJim Harris 
933b846efd7SJim Harris void
934b846efd7SJim Harris nvme_io_qpair_enable(struct nvme_qpair *qpair)
935b846efd7SJim Harris {
936b846efd7SJim Harris 	STAILQ_HEAD(, nvme_request)	temp;
937b846efd7SJim Harris 	struct nvme_tracker		*tr;
938cb5b7c13SJim Harris 	struct nvme_tracker		*tr_temp;
939b846efd7SJim Harris 	struct nvme_request		*req;
940b846efd7SJim Harris 
941cb5b7c13SJim Harris 	/*
942cb5b7c13SJim Harris 	 * Manually abort each outstanding I/O.  This normally results in a
943cb5b7c13SJim Harris 	 *  retry, unless the retry count on the associated request has
944cb5b7c13SJim Harris 	 *  reached its limit.
945cb5b7c13SJim Harris 	 */
946cb5b7c13SJim Harris 	TAILQ_FOREACH_SAFE(tr, &qpair->outstanding_tr, tailq, tr_temp) {
947547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "aborting outstanding i/o\n");
948cb5b7c13SJim Harris 		nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
949232e2edbSJim Harris 		    NVME_SC_ABORTED_BY_REQUEST, 0, TRUE);
950cb5b7c13SJim Harris 	}
951cb5b7c13SJim Harris 
952b846efd7SJim Harris 	mtx_lock(&qpair->lock);
953b846efd7SJim Harris 
954b846efd7SJim Harris 	nvme_qpair_enable(qpair);
955b846efd7SJim Harris 
956b846efd7SJim Harris 	STAILQ_INIT(&temp);
957b846efd7SJim Harris 	STAILQ_SWAP(&qpair->queued_req, &temp, nvme_request);
958b846efd7SJim Harris 
959b846efd7SJim Harris 	while (!STAILQ_EMPTY(&temp)) {
960b846efd7SJim Harris 		req = STAILQ_FIRST(&temp);
961b846efd7SJim Harris 		STAILQ_REMOVE_HEAD(&temp, stailq);
962547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "resubmitting queued i/o\n");
963547d523eSJim Harris 		nvme_qpair_print_command(qpair, &req->cmd);
964b846efd7SJim Harris 		_nvme_qpair_submit_request(qpair, req);
965b846efd7SJim Harris 	}
966b846efd7SJim Harris 
967b846efd7SJim Harris 	mtx_unlock(&qpair->lock);
968b846efd7SJim Harris }
969b846efd7SJim Harris 
970b846efd7SJim Harris static void
971b846efd7SJim Harris nvme_qpair_disable(struct nvme_qpair *qpair)
972b846efd7SJim Harris {
973b846efd7SJim Harris 	struct nvme_tracker *tr;
974b846efd7SJim Harris 
975b846efd7SJim Harris 	qpair->is_enabled = FALSE;
976b846efd7SJim Harris 	mtx_lock(&qpair->lock);
977b846efd7SJim Harris 	TAILQ_FOREACH(tr, &qpair->outstanding_tr, tailq)
978b846efd7SJim Harris 		callout_stop(&tr->timer);
979b846efd7SJim Harris 	mtx_unlock(&qpair->lock);
980b846efd7SJim Harris }
981b846efd7SJim Harris 
982b846efd7SJim Harris void
983b846efd7SJim Harris nvme_admin_qpair_disable(struct nvme_qpair *qpair)
984b846efd7SJim Harris {
985b846efd7SJim Harris 
986b846efd7SJim Harris 	nvme_qpair_disable(qpair);
987b846efd7SJim Harris 	nvme_admin_qpair_abort_aers(qpair);
988b846efd7SJim Harris }
989b846efd7SJim Harris 
990b846efd7SJim Harris void
991b846efd7SJim Harris nvme_io_qpair_disable(struct nvme_qpair *qpair)
992b846efd7SJim Harris {
993b846efd7SJim Harris 
994b846efd7SJim Harris 	nvme_qpair_disable(qpair);
995b846efd7SJim Harris }
996232e2edbSJim Harris 
997232e2edbSJim Harris void
998232e2edbSJim Harris nvme_qpair_fail(struct nvme_qpair *qpair)
999232e2edbSJim Harris {
1000232e2edbSJim Harris 	struct nvme_tracker		*tr;
1001232e2edbSJim Harris 	struct nvme_request		*req;
1002232e2edbSJim Harris 
1003*824073fbSWarner Losh 	if (!mtx_initialized(&qpair->lock))
1004*824073fbSWarner Losh 		return;
1005*824073fbSWarner Losh 
1006232e2edbSJim Harris 	mtx_lock(&qpair->lock);
1007232e2edbSJim Harris 
1008232e2edbSJim Harris 	while (!STAILQ_EMPTY(&qpair->queued_req)) {
1009232e2edbSJim Harris 		req = STAILQ_FIRST(&qpair->queued_req);
1010232e2edbSJim Harris 		STAILQ_REMOVE_HEAD(&qpair->queued_req, stailq);
1011547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "failing queued i/o\n");
1012232e2edbSJim Harris 		mtx_unlock(&qpair->lock);
1013232e2edbSJim Harris 		nvme_qpair_manual_complete_request(qpair, req, NVME_SCT_GENERIC,
1014232e2edbSJim Harris 		    NVME_SC_ABORTED_BY_REQUEST, TRUE);
1015232e2edbSJim Harris 		mtx_lock(&qpair->lock);
1016232e2edbSJim Harris 	}
1017232e2edbSJim Harris 
1018232e2edbSJim Harris 	/* Manually abort each outstanding I/O. */
1019232e2edbSJim Harris 	while (!TAILQ_EMPTY(&qpair->outstanding_tr)) {
1020232e2edbSJim Harris 		tr = TAILQ_FIRST(&qpair->outstanding_tr);
1021232e2edbSJim Harris 		/*
1022232e2edbSJim Harris 		 * Do not remove the tracker.  The abort_tracker path will
1023232e2edbSJim Harris 		 *  do that for us.
1024232e2edbSJim Harris 		 */
1025547d523eSJim Harris 		nvme_printf(qpair->ctrlr, "failing outstanding i/o\n");
1026232e2edbSJim Harris 		mtx_unlock(&qpair->lock);
1027232e2edbSJim Harris 		nvme_qpair_manual_complete_tracker(qpair, tr, NVME_SCT_GENERIC,
1028232e2edbSJim Harris 		    NVME_SC_ABORTED_BY_REQUEST, 1 /* do not retry */, TRUE);
1029232e2edbSJim Harris 		mtx_lock(&qpair->lock);
1030232e2edbSJim Harris 	}
1031232e2edbSJim Harris 
1032232e2edbSJim Harris 	mtx_unlock(&qpair->lock);
1033232e2edbSJim Harris }
1034232e2edbSJim Harris 
1035