aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/virtio/scsi/virtio_scsivar.h
blob: 064b1245a5655054c7695f2d44ff28d2f0b0ed1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2012, Bryan Venteicher <bryanv@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef _VIRTIO_SCSIVAR_H
#define _VIRTIO_SCSIVAR_H

struct vtscsi_softc;
struct vtscsi_request;

typedef void vtscsi_request_cb_t(struct vtscsi_softc *,
    struct vtscsi_request *);

struct vtscsi_statistics {
	unsigned long		scsi_cmd_timeouts;
	unsigned long		dequeue_no_requests;
};

struct vtscsi_softc {
	device_t		 vtscsi_dev;
	struct mtx		 vtscsi_mtx;
	uint64_t		 vtscsi_features;

	uint16_t		 vtscsi_flags;
#define VTSCSI_FLAG_INDIRECT		0x0001
#define VTSCSI_FLAG_BIDIRECTIONAL	0x0002
#define VTSCSI_FLAG_HOTPLUG		0x0004
#define VTSCSI_FLAG_RESET		0x0008
#define VTSCSI_FLAG_DETACH		0x0010

	uint16_t		 vtscsi_frozen;
#define VTSCSI_FROZEN_NO_REQUESTS	0x01
#define VTSCSI_FROZEN_REQUEST_VQ_FULL	0x02

	struct sglist		*vtscsi_sglist;

	struct virtqueue	*vtscsi_control_vq;
	struct virtqueue	*vtscsi_event_vq;
	struct virtqueue	*vtscsi_request_vq;

	struct cam_sim		*vtscsi_sim;
	struct cam_path		*vtscsi_path;

	int			 vtscsi_debug;
	int			 vtscsi_nrequests;
	int			 vtscsi_max_nsegs;
	int			 vtscsi_event_buf_size;

	TAILQ_HEAD(,vtscsi_request)
				 vtscsi_req_free;

	uint16_t		 vtscsi_max_channel;
	uint16_t		 vtscsi_max_target;
	uint32_t		 vtscsi_max_lun;

#define VTSCSI_NUM_EVENT_BUFS	4
	struct virtio_scsi_event
				 vtscsi_event_bufs[VTSCSI_NUM_EVENT_BUFS];

	struct vtscsi_statistics vtscsi_stats;
};

enum vtscsi_request_state {
	VTSCSI_REQ_STATE_FREE,
	VTSCSI_REQ_STATE_INUSE,
	VTSCSI_REQ_STATE_ABORTED,
	VTSCSI_REQ_STATE_TIMEDOUT
};

struct vtscsi_request {
	struct vtscsi_softc			*vsr_softc;
	union ccb				*vsr_ccb;
	vtscsi_request_cb_t			*vsr_complete;

	void					*vsr_ptr0;
/* Request when aborting a timedout command. */
#define vsr_timedout_req	vsr_ptr0

	enum vtscsi_request_state		 vsr_state;

	uint16_t				 vsr_flags;
#define VTSCSI_REQ_FLAG_POLLED		0x01
#define VTSCSI_REQ_FLAG_COMPLETE	0x02
#define VTSCSI_REQ_FLAG_TIMEOUT_SET	0x04

	union {
		struct virtio_scsi_cmd_req	 cmd;
		struct virtio_scsi_ctrl_tmf_req	 tmf;
		struct virtio_scsi_ctrl_an_req	 an;
	} vsr_ureq;
#define vsr_cmd_req 	vsr_ureq.cmd
#define vsr_tmf_req 	vsr_ureq.tmf
#define vsr_an_req	vsr_ureq.an

	/* Make request and response non-contiguous. */
	uint32_t				 vsr_pad;

	union {
		struct virtio_scsi_cmd_resp	 cmd;
		struct virtio_scsi_ctrl_tmf_resp tmf;
		struct virtio_scsi_ctrl_an_resp	 an;
	} vsr_uresp;
#define vsr_cmd_resp	vsr_uresp.cmd
#define vsr_tmf_resp	vsr_uresp.tmf
#define vsr_an_resp	vsr_uresp.an

	struct callout				 vsr_callout;

	TAILQ_ENTRY(vtscsi_request)		 vsr_link;
};

/* Private field in the CCB header that points to our request. */
#define ccbh_vtscsi_req	spriv_ptr0

/* Features desired/implemented by this driver. */
#define VTSCSI_FEATURES \
    (VIRTIO_SCSI_F_HOTPLUG		| \
     VIRTIO_RING_F_INDIRECT_DESC)

#define VTSCSI_MTX(_sc)			&(_sc)->vtscsi_mtx
#define VTSCSI_LOCK_INIT(_sc, _name) 	mtx_init(VTSCSI_MTX(_sc), _name, \
					    "VTSCSI Lock", MTX_DEF)
#define VTSCSI_LOCK(_sc)		mtx_lock(VTSCSI_MTX(_sc))
#define VTSCSI_UNLOCK(_sc)		mtx_unlock(VTSCSI_MTX(_sc))
#define VTSCSI_LOCK_OWNED(_sc)		mtx_assert(VTSCSI_MTX(_sc), MA_OWNED)
#define VTSCSI_LOCK_NOTOWNED(_sc) 	mtx_assert(VTSCSI_MTX(_sc), MA_NOTOWNED)
#define VTSCSI_LOCK_DESTROY(_sc)	mtx_destroy(VTSCSI_MTX(_sc))

/*
 * Reasons for either freezing or thawing the SIMQ.
 *
 * VirtIO SCSI is a bit unique in the sense that SCSI and TMF
 * commands go over different queues. Both queues are fed by
 * the same SIMQ, but we only freeze the SIMQ when the request
 * (SCSI) virtqueue is full, not caring if the control (TMF)
 * virtqueue unlikely gets full. However, both queues share the
 * same pool of requests, so the completion of a TMF command
 * could cause the SIMQ to be unfrozen.
 */
#define VTSCSI_REQUEST		0x01
#define VTSCSI_REQUEST_VQ	0x02

/* Debug trace levels. */
#define VTSCSI_INFO	0x01
#define VTSCSI_ERROR	0x02
#define VTSCSI_TRACE	0x04

#define vtscsi_dprintf(_sc, _level, _msg, _args ...) do { 		\
	if ((_sc)->vtscsi_debug & (_level))				\
		device_printf((_sc)->vtscsi_dev, "%s: "_msg,		\
		    __FUNCTION__, ##_args);				\
} while (0)

#define vtscsi_dprintf_req(_req, _level, _msg, _args ...) do {		\
	struct vtscsi_softc *__sc = (_req)->vsr_softc;			\
	if ((__sc)->vtscsi_debug & (_level))				\
		vtscsi_printf_req(_req, __FUNCTION__, _msg, ##_args);	\
} while (0)

/*
 * Set the status field in a CCB, optionally clearing non CCB_STATUS_* flags.
 */
#define vtscsi_set_ccb_status(_ccbh, _status, _mask) do {		\
	KASSERT(((_mask) & CAM_STATUS_MASK) == 0,			\
	    ("%s:%d bad mask: 0x%x", __FUNCTION__, __LINE__, (_mask)));	\
	(_ccbh)->status &= ~(CAM_STATUS_MASK | (_mask));		\
	(_ccbh)->status |= (_status);					\
} while (0)

/*
 * One segment each for the request and the response.
 */
#define VTSCSI_MIN_SEGMENTS	2

/*
 * Allocate additional requests for internal use such
 * as TM commands (e.g. aborting timedout commands).
 */
#define VTSCSI_RESERVED_REQUESTS	10

/*
 * How to wait (or not) for request completion.
 */
#define VTSCSI_EXECUTE_ASYNC	0
#define VTSCSI_EXECUTE_POLL	1

#endif /* _VIRTIO_SCSIVAR_H */