xref: /spdk/module/bdev/virtio/bdev_virtio_scsi.c (revision fa09c9ac9b0ce66dcacb64b02aae778014b6c2b3)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/bdev.h"
9 #include "spdk/endian.h"
10 #include "spdk/env.h"
11 #include "spdk/thread.h"
12 #include "spdk/scsi_spec.h"
13 #include "spdk/string.h"
14 #include "spdk/util.h"
15 #include "spdk/json.h"
16 
17 #include "spdk/bdev_module.h"
18 #include "spdk/log.h"
19 #include "spdk_internal/virtio.h"
20 #include "spdk_internal/vhost_user.h"
21 
22 #include <linux/virtio_scsi.h>
23 #include <linux/virtio_ids.h>
24 
25 #include "bdev_virtio.h"
26 
27 #define BDEV_VIRTIO_MAX_TARGET 64
28 #define BDEV_VIRTIO_SCAN_PAYLOAD_SIZE 256
29 #define MGMT_POLL_PERIOD_US (1000 * 5)
30 #define CTRLQ_RING_SIZE 16
31 #define SCAN_REQUEST_RETRIES 5
32 
33 /* Number of non-request queues - eventq and controlq */
34 #define SPDK_VIRTIO_SCSI_QUEUE_NUM_FIXED 2
35 
36 #define VIRTIO_SCSI_EVENTQ_BUFFER_COUNT 16
37 
38 #define VIRTIO_SCSI_CONTROLQ	0
39 #define VIRTIO_SCSI_EVENTQ	1
40 #define VIRTIO_SCSI_REQUESTQ	2
41 
42 static int bdev_virtio_initialize(void);
43 static void bdev_virtio_finish(void);
44 
45 struct virtio_scsi_dev {
46 	/* Generic virtio device data. */
47 	struct virtio_dev		vdev;
48 
49 	/** Detected SCSI LUNs */
50 	TAILQ_HEAD(, virtio_scsi_disk)	luns;
51 
52 	/** Context for the SCSI target scan. */
53 	struct virtio_scsi_scan_base	*scan_ctx;
54 
55 	/** Controlq poller. */
56 	struct spdk_poller		*mgmt_poller;
57 
58 	/** Controlq messages to be sent. */
59 	struct spdk_ring		*ctrlq_ring;
60 
61 	/** Buffers for the eventq. */
62 	struct virtio_scsi_eventq_io	*eventq_ios;
63 
64 	/** Device marked for removal. */
65 	bool				removed;
66 
67 	/** Callback to be called after vdev removal. */
68 	bdev_virtio_remove_cb		remove_cb;
69 
70 	/** Context for the `remove_cb`. */
71 	void				*remove_ctx;
72 
73 	TAILQ_ENTRY(virtio_scsi_dev) tailq;
74 };
75 
76 struct virtio_scsi_io_ctx {
77 	struct iovec			iov_req;
78 	struct iovec			iov_resp;
79 	union {
80 		struct virtio_scsi_cmd_req req;
81 		struct virtio_scsi_ctrl_tmf_req tmf_req;
82 	};
83 	union {
84 		struct virtio_scsi_cmd_resp resp;
85 		struct virtio_scsi_ctrl_tmf_resp tmf_resp;
86 	};
87 };
88 
89 struct virtio_scsi_eventq_io {
90 	struct iovec			iov;
91 	struct virtio_scsi_event	ev;
92 };
93 
94 struct virtio_scsi_scan_info {
95 	uint64_t			num_blocks;
96 	uint32_t			block_size;
97 	uint8_t				target;
98 	bool				unmap_supported;
99 	TAILQ_ENTRY(virtio_scsi_scan_info) tailq;
100 };
101 
102 struct virtio_scsi_scan_base {
103 	struct virtio_scsi_dev		*svdev;
104 
105 	/** I/O channel used for the scan I/O. */
106 	struct bdev_virtio_io_channel	*channel;
107 
108 	bdev_virtio_create_cb		cb_fn;
109 	void				*cb_arg;
110 
111 	/** Scan all targets on the device. */
112 	bool				full_scan;
113 
114 	/** Start a full rescan after receiving next scan I/O response. */
115 	bool				restart;
116 
117 	/** Additional targets to be (re)scanned. */
118 	TAILQ_HEAD(, virtio_scsi_scan_info) scan_queue;
119 
120 	/** Remaining attempts for sending the current request. */
121 	unsigned                        retries;
122 
123 	/** If set, the last scan I/O needs to be resent */
124 	bool				needs_resend;
125 
126 	struct virtio_scsi_io_ctx	io_ctx;
127 	struct iovec			iov;
128 	uint8_t				payload[BDEV_VIRTIO_SCAN_PAYLOAD_SIZE];
129 
130 	/** Scan results for the current target. */
131 	struct virtio_scsi_scan_info	info;
132 };
133 
134 struct virtio_scsi_disk {
135 	struct spdk_bdev		bdev;
136 	struct virtio_scsi_dev		*svdev;
137 	struct virtio_scsi_scan_info	info;
138 
139 	/** Descriptor opened just to be notified of external bdev hotremove. */
140 	struct spdk_bdev_desc		*notify_desc;
141 
142 	/** Disk marked for removal. */
143 	bool				removed;
144 	TAILQ_ENTRY(virtio_scsi_disk)	link;
145 };
146 
147 struct bdev_virtio_io_channel {
148 	struct virtio_scsi_dev	*svdev;
149 
150 	/** Virtqueue exclusively assigned to this channel. */
151 	struct virtqueue	*vq;
152 
153 	/** Virtio response poller. */
154 	struct spdk_poller	*poller;
155 };
156 
157 static TAILQ_HEAD(, virtio_scsi_dev) g_virtio_scsi_devs =
158 	TAILQ_HEAD_INITIALIZER(g_virtio_scsi_devs);
159 
160 static pthread_mutex_t g_virtio_scsi_mutex = PTHREAD_MUTEX_INITIALIZER;
161 
162 /** Module finish in progress */
163 static bool g_bdev_virtio_finish = false;
164 
165 /* Features desired/implemented by this driver. */
166 #define VIRTIO_SCSI_DEV_SUPPORTED_FEATURES		\
167 	(1ULL << VIRTIO_SCSI_F_INOUT		|	\
168 	 1ULL << VIRTIO_SCSI_F_HOTPLUG		|	\
169 	 1ULL << VIRTIO_RING_F_EVENT_IDX)
170 
171 static void virtio_scsi_dev_unregister_cb(void *io_device);
172 static void virtio_scsi_dev_remove(struct virtio_scsi_dev *svdev,
173 				   bdev_virtio_remove_cb cb_fn, void *cb_arg);
174 static int bdev_virtio_scsi_ch_create_cb(void *io_device, void *ctx_buf);
175 static void bdev_virtio_scsi_ch_destroy_cb(void *io_device, void *ctx_buf);
176 static void process_scan_resp(struct virtio_scsi_scan_base *base);
177 static int bdev_virtio_mgmt_poll(void *arg);
178 
179 static int
180 virtio_scsi_dev_send_eventq_io(struct virtqueue *vq, struct virtio_scsi_eventq_io *io)
181 {
182 	int rc;
183 
184 	rc = virtqueue_req_start(vq, io, 1);
185 	if (rc != 0) {
186 		return -1;
187 	}
188 
189 	virtqueue_req_add_iovs(vq, &io->iov, 1, SPDK_VIRTIO_DESC_WR);
190 	virtqueue_req_flush(vq);
191 
192 	return 0;
193 }
194 
195 static int
196 virtio_scsi_dev_init(struct virtio_scsi_dev *svdev, uint16_t max_queues, uint64_t feature_bits)
197 {
198 	struct virtio_dev *vdev = &svdev->vdev;
199 	struct spdk_ring *ctrlq_ring;
200 	struct virtio_scsi_eventq_io *eventq_io;
201 	struct virtqueue *eventq;
202 	uint16_t i, num_events;
203 	int rc;
204 
205 	rc = virtio_dev_reset(vdev, feature_bits);
206 	if (rc != 0) {
207 		return rc;
208 	}
209 
210 	rc = virtio_dev_start(vdev, max_queues, SPDK_VIRTIO_SCSI_QUEUE_NUM_FIXED);
211 	if (rc != 0) {
212 		return rc;
213 	}
214 
215 	ctrlq_ring = spdk_ring_create(SPDK_RING_TYPE_MP_SC, CTRLQ_RING_SIZE,
216 				      SPDK_ENV_SOCKET_ID_ANY);
217 	if (ctrlq_ring == NULL) {
218 		SPDK_ERRLOG("Failed to allocate send ring for the controlq.\n");
219 		return -1;
220 	}
221 
222 	rc = virtio_dev_acquire_queue(vdev, VIRTIO_SCSI_CONTROLQ);
223 	if (rc != 0) {
224 		SPDK_ERRLOG("Failed to acquire the controlq.\n");
225 		spdk_ring_free(ctrlq_ring);
226 		return -1;
227 	}
228 
229 	rc = virtio_dev_acquire_queue(vdev, VIRTIO_SCSI_EVENTQ);
230 	if (rc != 0) {
231 		SPDK_ERRLOG("Failed to acquire the eventq.\n");
232 		virtio_dev_release_queue(vdev, VIRTIO_SCSI_CONTROLQ);
233 		spdk_ring_free(ctrlq_ring);
234 		return -1;
235 	}
236 
237 	eventq = vdev->vqs[VIRTIO_SCSI_EVENTQ];
238 	num_events = spdk_min(eventq->vq_nentries, VIRTIO_SCSI_EVENTQ_BUFFER_COUNT);
239 	svdev->eventq_ios = spdk_zmalloc(sizeof(*svdev->eventq_ios) * num_events,
240 					 0, NULL, SPDK_ENV_LCORE_ID_ANY,
241 					 SPDK_MALLOC_DMA);
242 	if (svdev->eventq_ios == NULL) {
243 		SPDK_ERRLOG("cannot allocate memory for %"PRIu16" eventq buffers\n",
244 			    num_events);
245 		virtio_dev_release_queue(vdev, VIRTIO_SCSI_EVENTQ);
246 		virtio_dev_release_queue(vdev, VIRTIO_SCSI_CONTROLQ);
247 		spdk_ring_free(ctrlq_ring);
248 		return -1;
249 	}
250 
251 	for (i = 0; i < num_events; i++) {
252 		eventq_io = &svdev->eventq_ios[i];
253 		eventq_io->iov.iov_base = &eventq_io->ev;
254 		eventq_io->iov.iov_len = sizeof(eventq_io->ev);
255 		virtio_scsi_dev_send_eventq_io(eventq, eventq_io);
256 	}
257 
258 	svdev->ctrlq_ring = ctrlq_ring;
259 
260 	svdev->mgmt_poller = SPDK_POLLER_REGISTER(bdev_virtio_mgmt_poll, svdev,
261 			     MGMT_POLL_PERIOD_US);
262 
263 	TAILQ_INIT(&svdev->luns);
264 	svdev->scan_ctx = NULL;
265 	svdev->removed = false;
266 	svdev->remove_cb = NULL;
267 	svdev->remove_ctx = NULL;
268 
269 	spdk_io_device_register(svdev, bdev_virtio_scsi_ch_create_cb,
270 				bdev_virtio_scsi_ch_destroy_cb,
271 				sizeof(struct bdev_virtio_io_channel),
272 				svdev->vdev.name);
273 
274 	pthread_mutex_lock(&g_virtio_scsi_mutex);
275 	TAILQ_INSERT_TAIL(&g_virtio_scsi_devs, svdev, tailq);
276 	pthread_mutex_unlock(&g_virtio_scsi_mutex);
277 	return 0;
278 }
279 
280 static struct virtio_scsi_dev *
281 virtio_pci_scsi_dev_create(const char *name, struct virtio_pci_ctx *pci_ctx)
282 {
283 	static int pci_dev_counter = 0;
284 	struct virtio_scsi_dev *svdev;
285 	struct virtio_dev *vdev;
286 	char *default_name = NULL;
287 	uint32_t num_queues;
288 	int rc;
289 
290 	svdev = calloc(1, sizeof(*svdev));
291 	if (svdev == NULL) {
292 		SPDK_ERRLOG("virtio device calloc failed\n");
293 		return NULL;
294 	}
295 
296 	vdev = &svdev->vdev;
297 	if (name == NULL) {
298 		default_name = spdk_sprintf_alloc("VirtioScsi%"PRIu32, pci_dev_counter++);
299 		if (default_name == NULL) {
300 			free(vdev);
301 			return NULL;
302 		}
303 		name = default_name;
304 	}
305 
306 	rc = virtio_pci_dev_init(vdev, name, pci_ctx);
307 	free(default_name);
308 
309 	if (rc != 0) {
310 		free(svdev);
311 		return NULL;
312 	}
313 
314 	rc = virtio_dev_read_dev_config(vdev, offsetof(struct virtio_scsi_config, num_queues),
315 					&num_queues, sizeof(num_queues));
316 	if (rc) {
317 		SPDK_ERRLOG("%s: config read failed: %s\n", vdev->name, spdk_strerror(-rc));
318 		goto fail;
319 	}
320 
321 	rc = virtio_scsi_dev_init(svdev, num_queues, VIRTIO_SCSI_DEV_SUPPORTED_FEATURES);
322 	if (rc != 0) {
323 		goto fail;
324 	}
325 
326 	return svdev;
327 
328 fail:
329 	vdev->ctx = NULL;
330 	virtio_dev_destruct(vdev);
331 	free(svdev);
332 	return NULL;
333 }
334 
335 static struct virtio_scsi_dev *
336 virtio_user_scsi_dev_create(const char *name, const char *path,
337 			    uint16_t num_queues, uint32_t queue_size)
338 {
339 	struct virtio_scsi_dev *svdev;
340 	struct virtio_dev *vdev;
341 	uint64_t feature_bits;
342 	int rc;
343 
344 	svdev = calloc(1, sizeof(*svdev));
345 	if (svdev == NULL) {
346 		SPDK_ERRLOG("calloc failed for virtio device %s: %s\n", name, path);
347 		return NULL;
348 	}
349 
350 	vdev = &svdev->vdev;
351 	rc = virtio_user_dev_init(vdev, name, path, queue_size);
352 	if (rc != 0) {
353 		SPDK_ERRLOG("Failed to create virito device %s: %s\n", name, path);
354 		free(svdev);
355 		return NULL;
356 	}
357 
358 	feature_bits = VIRTIO_SCSI_DEV_SUPPORTED_FEATURES;
359 	feature_bits |= (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
360 	rc = virtio_scsi_dev_init(svdev, num_queues + SPDK_VIRTIO_SCSI_QUEUE_NUM_FIXED, feature_bits);
361 	if (rc != 0) {
362 		virtio_dev_destruct(vdev);
363 		free(svdev);
364 		return NULL;
365 	}
366 
367 	return svdev;
368 }
369 
370 static struct virtio_scsi_disk *
371 virtio_scsi_dev_get_disk_by_id(struct virtio_scsi_dev *svdev, uint8_t target_id)
372 {
373 	struct virtio_scsi_disk *disk;
374 
375 	TAILQ_FOREACH(disk, &svdev->luns, link) {
376 		if (disk->info.target == target_id) {
377 			return disk;
378 		}
379 	}
380 
381 	return NULL;
382 }
383 
384 static int virtio_scsi_dev_scan(struct virtio_scsi_dev *svdev,
385 				bdev_virtio_create_cb cb_fn, void *cb_arg);
386 static int send_scan_io(struct virtio_scsi_scan_base *base);
387 static void _virtio_scsi_dev_scan_tgt(struct virtio_scsi_scan_base *base, uint8_t target);
388 static int _virtio_scsi_dev_scan_next(struct virtio_scsi_scan_base *base, int rc);
389 static void _virtio_scsi_dev_scan_finish(struct virtio_scsi_scan_base *base, int errnum);
390 static int virtio_scsi_dev_scan_tgt(struct virtio_scsi_dev *svdev, uint8_t target);
391 
392 static int
393 bdev_virtio_get_ctx_size(void)
394 {
395 	return sizeof(struct virtio_scsi_io_ctx);
396 }
397 
398 static int
399 bdev_virtio_scsi_config_json(struct spdk_json_write_ctx *w)
400 {
401 	struct virtio_scsi_dev *svdev;
402 
403 	pthread_mutex_lock(&g_virtio_scsi_mutex);
404 	TAILQ_FOREACH(svdev, &g_virtio_scsi_devs, tailq) {
405 		spdk_json_write_object_begin(w);
406 
407 		spdk_json_write_named_string(w, "method", "bdev_virtio_attach_controller");
408 
409 		spdk_json_write_named_object_begin(w, "params");
410 		spdk_json_write_named_string(w, "name", svdev->vdev.name);
411 		spdk_json_write_named_string(w, "dev_type", "scsi");
412 
413 		/* Write transport specific parameters. */
414 		svdev->vdev.backend_ops->write_json_config(&svdev->vdev, w);
415 
416 		spdk_json_write_object_end(w);
417 
418 		spdk_json_write_object_end(w);
419 
420 	}
421 	pthread_mutex_unlock(&g_virtio_scsi_mutex);
422 
423 	return 0;
424 }
425 
426 
427 static struct spdk_bdev_module virtio_scsi_if = {
428 	.name = "virtio_scsi",
429 	.module_init = bdev_virtio_initialize,
430 	.module_fini = bdev_virtio_finish,
431 	.get_ctx_size = bdev_virtio_get_ctx_size,
432 	.config_json = bdev_virtio_scsi_config_json,
433 	.async_fini = true,
434 };
435 
436 SPDK_BDEV_MODULE_REGISTER(virtio_scsi, &virtio_scsi_if)
437 
438 static struct virtio_scsi_io_ctx *
439 bdev_virtio_init_io_vreq(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
440 {
441 	struct virtio_scsi_cmd_req *req;
442 	struct virtio_scsi_cmd_resp *resp;
443 	struct virtio_scsi_disk *disk = (struct virtio_scsi_disk *)bdev_io->bdev;
444 	struct virtio_scsi_io_ctx *io_ctx = (struct virtio_scsi_io_ctx *)bdev_io->driver_ctx;
445 
446 	req = &io_ctx->req;
447 	resp = &io_ctx->resp;
448 
449 	io_ctx->iov_req.iov_base = req;
450 	io_ctx->iov_req.iov_len = sizeof(*req);
451 
452 	io_ctx->iov_resp.iov_base = resp;
453 	io_ctx->iov_resp.iov_len = sizeof(*resp);
454 
455 	memset(req, 0, sizeof(*req));
456 	req->lun[0] = 1;
457 	req->lun[1] = disk->info.target;
458 
459 	return io_ctx;
460 }
461 
462 static struct virtio_scsi_io_ctx *
463 bdev_virtio_init_tmf_vreq(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
464 {
465 	struct virtio_scsi_ctrl_tmf_req *tmf_req;
466 	struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
467 	struct virtio_scsi_disk *disk = SPDK_CONTAINEROF(bdev_io->bdev, struct virtio_scsi_disk, bdev);
468 	struct virtio_scsi_io_ctx *io_ctx = (struct virtio_scsi_io_ctx *)bdev_io->driver_ctx;
469 
470 	tmf_req = &io_ctx->tmf_req;
471 	tmf_resp = &io_ctx->tmf_resp;
472 
473 	io_ctx->iov_req.iov_base = tmf_req;
474 	io_ctx->iov_req.iov_len = sizeof(*tmf_req);
475 	io_ctx->iov_resp.iov_base = tmf_resp;
476 	io_ctx->iov_resp.iov_len = sizeof(*tmf_resp);
477 
478 	memset(tmf_req, 0, sizeof(*tmf_req));
479 	tmf_req->lun[0] = 1;
480 	tmf_req->lun[1] = disk->info.target;
481 
482 	return io_ctx;
483 }
484 
485 static void
486 bdev_virtio_send_io(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
487 {
488 	struct bdev_virtio_io_channel *virtio_channel = spdk_io_channel_get_ctx(ch);
489 	struct virtqueue *vq = virtio_channel->vq;
490 	struct virtio_scsi_io_ctx *io_ctx = (struct virtio_scsi_io_ctx *)bdev_io->driver_ctx;
491 	int rc;
492 
493 	rc = virtqueue_req_start(vq, bdev_io, bdev_io->u.bdev.iovcnt + 2);
494 	if (rc == -ENOMEM) {
495 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
496 		return;
497 	} else if (rc != 0) {
498 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
499 		return;
500 	}
501 
502 	virtqueue_req_add_iovs(vq, &io_ctx->iov_req, 1, SPDK_VIRTIO_DESC_RO);
503 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
504 		virtqueue_req_add_iovs(vq, &io_ctx->iov_resp, 1, SPDK_VIRTIO_DESC_WR);
505 		virtqueue_req_add_iovs(vq, bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
506 				       SPDK_VIRTIO_DESC_WR);
507 	} else {
508 		virtqueue_req_add_iovs(vq, bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
509 				       SPDK_VIRTIO_DESC_RO);
510 		virtqueue_req_add_iovs(vq, &io_ctx->iov_resp, 1, SPDK_VIRTIO_DESC_WR);
511 	}
512 
513 	virtqueue_req_flush(vq);
514 }
515 
516 static void
517 bdev_virtio_rw(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
518 {
519 	struct virtio_scsi_disk *disk = SPDK_CONTAINEROF(bdev_io->bdev, struct virtio_scsi_disk, bdev);
520 	struct virtio_scsi_io_ctx *io_ctx = bdev_virtio_init_io_vreq(ch, bdev_io);
521 	struct virtio_scsi_cmd_req *req = &io_ctx->req;
522 	bool is_write = bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE;
523 
524 	if (disk->info.num_blocks > (1ULL << 32)) {
525 		req->cdb[0] = is_write ? SPDK_SBC_WRITE_16 : SPDK_SBC_READ_16;
526 		to_be64(&req->cdb[2], bdev_io->u.bdev.offset_blocks);
527 		to_be32(&req->cdb[10], bdev_io->u.bdev.num_blocks);
528 	} else {
529 		req->cdb[0] = is_write ? SPDK_SBC_WRITE_10 : SPDK_SBC_READ_10;
530 		to_be32(&req->cdb[2], bdev_io->u.bdev.offset_blocks);
531 		to_be16(&req->cdb[7], bdev_io->u.bdev.num_blocks);
532 	}
533 
534 	bdev_virtio_send_io(ch, bdev_io);
535 }
536 
537 static void
538 bdev_virtio_reset(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
539 {
540 	struct bdev_virtio_io_channel *virtio_ch = spdk_io_channel_get_ctx(ch);
541 	struct virtio_scsi_io_ctx *io_ctx = bdev_virtio_init_tmf_vreq(ch, bdev_io);
542 	struct virtio_scsi_ctrl_tmf_req *tmf_req = &io_ctx->tmf_req;
543 	struct virtio_scsi_dev *svdev = virtio_ch->svdev;
544 	size_t enqueued_count;
545 
546 	tmf_req->type = VIRTIO_SCSI_T_TMF;
547 	tmf_req->subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET;
548 
549 	enqueued_count = spdk_ring_enqueue(svdev->ctrlq_ring, (void **)&bdev_io, 1, NULL);
550 	if (spdk_likely(enqueued_count == 1)) {
551 		return;
552 	} else {
553 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
554 	}
555 }
556 
557 static void
558 bdev_virtio_unmap(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success)
559 {
560 	struct virtio_scsi_io_ctx *io_ctx = bdev_virtio_init_io_vreq(ch, bdev_io);
561 	struct virtio_scsi_cmd_req *req = &io_ctx->req;
562 	struct spdk_scsi_unmap_bdesc *desc, *first_desc;
563 	uint8_t *buf;
564 	uint64_t offset_blocks, num_blocks;
565 	uint16_t cmd_len;
566 
567 	if (!success) {
568 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
569 		return;
570 	}
571 
572 	buf = bdev_io->u.bdev.iovs[0].iov_base;
573 
574 	offset_blocks = bdev_io->u.bdev.offset_blocks;
575 	num_blocks = bdev_io->u.bdev.num_blocks;
576 
577 	/* (n-1) * 16-byte descriptors */
578 	first_desc = desc = (struct spdk_scsi_unmap_bdesc *)&buf[8];
579 	while (num_blocks > UINT32_MAX) {
580 		to_be64(&desc->lba, offset_blocks);
581 		to_be32(&desc->block_count, UINT32_MAX);
582 		memset(&desc->reserved, 0, sizeof(desc->reserved));
583 		offset_blocks += UINT32_MAX;
584 		num_blocks -= UINT32_MAX;
585 		desc++;
586 	}
587 
588 	/* The last descriptor with block_count <= UINT32_MAX */
589 	to_be64(&desc->lba, offset_blocks);
590 	to_be32(&desc->block_count, num_blocks);
591 	memset(&desc->reserved, 0, sizeof(desc->reserved));
592 
593 	/* 8-byte header + n * 16-byte block descriptor */
594 	cmd_len = 8 + (desc - first_desc + 1) *  sizeof(struct spdk_scsi_unmap_bdesc);
595 
596 	req->cdb[0] = SPDK_SBC_UNMAP;
597 	to_be16(&req->cdb[7], cmd_len);
598 
599 	/* 8-byte header */
600 	to_be16(&buf[0], cmd_len - 2); /* total length (excluding the length field) */
601 	to_be16(&buf[2], cmd_len - 8); /* length of block descriptors */
602 	memset(&buf[4], 0, 4); /* reserved */
603 
604 	bdev_virtio_send_io(ch, bdev_io);
605 }
606 
607 static void
608 bdev_virtio_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
609 		       bool success)
610 {
611 	if (!success) {
612 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
613 		return;
614 	}
615 
616 	bdev_virtio_rw(ch, bdev_io);
617 }
618 
619 static int
620 _bdev_virtio_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
621 {
622 	struct virtio_scsi_disk *disk = SPDK_CONTAINEROF(bdev_io->bdev, struct virtio_scsi_disk, bdev);
623 
624 	switch (bdev_io->type) {
625 	case SPDK_BDEV_IO_TYPE_READ:
626 		spdk_bdev_io_get_buf(bdev_io, bdev_virtio_get_buf_cb,
627 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
628 		return 0;
629 	case SPDK_BDEV_IO_TYPE_WRITE:
630 		bdev_virtio_rw(ch, bdev_io);
631 		return 0;
632 	case SPDK_BDEV_IO_TYPE_RESET:
633 		bdev_virtio_reset(ch, bdev_io);
634 		return 0;
635 	case SPDK_BDEV_IO_TYPE_UNMAP: {
636 		uint64_t buf_len = 8 /* header size */ +
637 				   (bdev_io->u.bdev.num_blocks + UINT32_MAX - 1) /
638 				   UINT32_MAX * sizeof(struct spdk_scsi_unmap_bdesc);
639 
640 		if (!disk->info.unmap_supported) {
641 			return -1;
642 		}
643 
644 		if (buf_len > SPDK_BDEV_LARGE_BUF_MAX_SIZE) {
645 			SPDK_ERRLOG("Trying to UNMAP too many blocks: %"PRIu64"\n",
646 				    bdev_io->u.bdev.num_blocks);
647 			return -1;
648 		}
649 		spdk_bdev_io_get_buf(bdev_io, bdev_virtio_unmap, buf_len);
650 		return 0;
651 	}
652 	case SPDK_BDEV_IO_TYPE_FLUSH:
653 	default:
654 		return -1;
655 	}
656 	return 0;
657 }
658 
659 static void
660 bdev_virtio_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
661 {
662 	if (_bdev_virtio_submit_request(ch, bdev_io) < 0) {
663 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
664 	}
665 }
666 
667 static bool
668 bdev_virtio_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
669 {
670 	struct virtio_scsi_disk *disk = ctx;
671 
672 	switch (io_type) {
673 	case SPDK_BDEV_IO_TYPE_READ:
674 	case SPDK_BDEV_IO_TYPE_WRITE:
675 	case SPDK_BDEV_IO_TYPE_FLUSH:
676 	case SPDK_BDEV_IO_TYPE_RESET:
677 		return true;
678 
679 	case SPDK_BDEV_IO_TYPE_UNMAP:
680 		return disk->info.unmap_supported;
681 
682 	default:
683 		return false;
684 	}
685 }
686 
687 static struct spdk_io_channel *
688 bdev_virtio_get_io_channel(void *ctx)
689 {
690 	struct virtio_scsi_disk *disk = ctx;
691 
692 	return spdk_get_io_channel(disk->svdev);
693 }
694 
695 static int
696 bdev_virtio_disk_destruct(void *ctx)
697 {
698 	struct virtio_scsi_disk *disk = ctx;
699 	struct virtio_scsi_dev *svdev = disk->svdev;
700 
701 	TAILQ_REMOVE(&svdev->luns, disk, link);
702 	free(disk->bdev.name);
703 	free(disk);
704 
705 	if (svdev->removed && TAILQ_EMPTY(&svdev->luns)) {
706 		spdk_io_device_unregister(svdev, virtio_scsi_dev_unregister_cb);
707 	}
708 
709 	return 0;
710 }
711 
712 static int
713 bdev_virtio_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
714 {
715 	struct virtio_scsi_disk *disk = ctx;
716 
717 	virtio_dev_dump_json_info(&disk->svdev->vdev, w);
718 	return 0;
719 }
720 
721 static void
722 bdev_virtio_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
723 {
724 	/* SCSI targets and LUNS are discovered during scan process so nothing
725 	 * to save here.
726 	 */
727 }
728 
729 static const struct spdk_bdev_fn_table virtio_fn_table = {
730 	.destruct		= bdev_virtio_disk_destruct,
731 	.submit_request		= bdev_virtio_submit_request,
732 	.io_type_supported	= bdev_virtio_io_type_supported,
733 	.get_io_channel		= bdev_virtio_get_io_channel,
734 	.dump_info_json		= bdev_virtio_dump_info_json,
735 	.write_config_json	= bdev_virtio_write_config_json,
736 };
737 
738 static void
739 get_scsi_status(struct virtio_scsi_cmd_resp *resp, int *sk, int *asc, int *ascq)
740 {
741 	/* see spdk_scsi_task_build_sense_data() for sense data details */
742 	*sk = 0;
743 	*asc = 0;
744 	*ascq = 0;
745 
746 	if (resp->sense_len < 3) {
747 		return;
748 	}
749 
750 	*sk = resp->sense[2] & 0xf;
751 
752 	if (resp->sense_len < 13) {
753 		return;
754 	}
755 
756 	*asc = resp->sense[12];
757 
758 	if (resp->sense_len < 14) {
759 		return;
760 	}
761 
762 	*ascq = resp->sense[13];
763 }
764 
765 static void
766 bdev_virtio_io_cpl(struct spdk_bdev_io *bdev_io)
767 {
768 	struct virtio_scsi_io_ctx *io_ctx = (struct virtio_scsi_io_ctx *)bdev_io->driver_ctx;
769 	int sk, asc, ascq;
770 
771 	get_scsi_status(&io_ctx->resp, &sk, &asc, &ascq);
772 	spdk_bdev_io_complete_scsi_status(bdev_io, io_ctx->resp.status, sk, asc, ascq);
773 }
774 
775 static int
776 bdev_virtio_poll(void *arg)
777 {
778 	struct bdev_virtio_io_channel *ch = arg;
779 	struct virtio_scsi_dev *svdev = ch->svdev;
780 	struct virtio_scsi_scan_base *scan_ctx = svdev->scan_ctx;
781 	void *io[32];
782 	uint32_t io_len[32];
783 	uint16_t i, cnt;
784 	int rc;
785 
786 	cnt = virtio_recv_pkts(ch->vq, (void **)io, io_len, SPDK_COUNTOF(io));
787 	for (i = 0; i < cnt; ++i) {
788 		if (spdk_unlikely(scan_ctx && io[i] == &scan_ctx->io_ctx)) {
789 			if (svdev->removed) {
790 				_virtio_scsi_dev_scan_finish(scan_ctx, -EINTR);
791 				return SPDK_POLLER_BUSY;
792 			}
793 
794 			if (scan_ctx->restart) {
795 				scan_ctx->restart = false;
796 				scan_ctx->full_scan = true;
797 				_virtio_scsi_dev_scan_tgt(scan_ctx, 0);
798 				continue;
799 			}
800 
801 			process_scan_resp(scan_ctx);
802 			continue;
803 		}
804 
805 		bdev_virtio_io_cpl(io[i]);
806 	}
807 
808 	if (spdk_unlikely(scan_ctx && scan_ctx->needs_resend)) {
809 		if (svdev->removed) {
810 			_virtio_scsi_dev_scan_finish(scan_ctx, -EINTR);
811 			return SPDK_POLLER_BUSY;
812 		} else if (cnt == 0) {
813 			return SPDK_POLLER_IDLE;
814 		}
815 
816 		rc = send_scan_io(scan_ctx);
817 		if (rc != 0) {
818 			assert(scan_ctx->retries > 0);
819 			scan_ctx->retries--;
820 			if (scan_ctx->retries == 0) {
821 				SPDK_ERRLOG("Target scan failed unrecoverably with rc = %d.\n", rc);
822 				_virtio_scsi_dev_scan_finish(scan_ctx, rc);
823 			}
824 		}
825 	}
826 
827 	return cnt;
828 }
829 
830 static void
831 bdev_virtio_tmf_cpl_cb(void *ctx)
832 {
833 	struct spdk_bdev_io *bdev_io = ctx;
834 	struct virtio_scsi_io_ctx *io_ctx = (struct virtio_scsi_io_ctx *)bdev_io->driver_ctx;
835 
836 	if (io_ctx->tmf_resp.response == VIRTIO_SCSI_S_OK) {
837 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
838 	} else {
839 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
840 	}
841 }
842 
843 static void
844 bdev_virtio_tmf_cpl(struct spdk_bdev_io *bdev_io)
845 {
846 	spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), bdev_virtio_tmf_cpl_cb, bdev_io);
847 }
848 
849 static void
850 bdev_virtio_eventq_io_cpl(struct virtio_scsi_dev *svdev, struct virtio_scsi_eventq_io *io)
851 {
852 	struct virtio_scsi_event *ev = &io->ev;
853 	struct virtio_scsi_disk *disk;
854 
855 	if (ev->lun[0] != 1) {
856 		SPDK_WARNLOG("Received an event with invalid data layout.\n");
857 		goto out;
858 	}
859 
860 	if (ev->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
861 		ev->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
862 		virtio_scsi_dev_scan(svdev, NULL, NULL);
863 	}
864 
865 	switch (ev->event) {
866 	case VIRTIO_SCSI_T_NO_EVENT:
867 		break;
868 	case VIRTIO_SCSI_T_TRANSPORT_RESET:
869 		switch (ev->reason) {
870 		case VIRTIO_SCSI_EVT_RESET_RESCAN:
871 			virtio_scsi_dev_scan_tgt(svdev, ev->lun[1]);
872 			break;
873 		case VIRTIO_SCSI_EVT_RESET_REMOVED:
874 			disk = virtio_scsi_dev_get_disk_by_id(svdev, ev->lun[1]);
875 			if (disk != NULL) {
876 				spdk_bdev_unregister(&disk->bdev, NULL, NULL);
877 			}
878 			break;
879 		default:
880 			break;
881 		}
882 		break;
883 	default:
884 		break;
885 	}
886 
887 out:
888 	virtio_scsi_dev_send_eventq_io(svdev->vdev.vqs[VIRTIO_SCSI_EVENTQ], io);
889 }
890 
891 static void
892 bdev_virtio_tmf_abort_nomem_cb(void *ctx)
893 {
894 	struct spdk_bdev_io *bdev_io = ctx;
895 
896 	spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
897 }
898 
899 static void
900 bdev_virtio_tmf_abort_ioerr_cb(void *ctx)
901 {
902 	struct spdk_bdev_io *bdev_io = ctx;
903 
904 	spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
905 }
906 
907 static void
908 bdev_virtio_tmf_abort(struct spdk_bdev_io *bdev_io, int status)
909 {
910 	spdk_msg_fn fn;
911 
912 	if (status == -ENOMEM) {
913 		fn = bdev_virtio_tmf_abort_nomem_cb;
914 	} else {
915 		fn = bdev_virtio_tmf_abort_ioerr_cb;
916 	}
917 
918 	spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), fn, bdev_io);
919 }
920 
921 static int
922 bdev_virtio_send_tmf_io(struct virtqueue *ctrlq, struct spdk_bdev_io *bdev_io)
923 {
924 	struct virtio_scsi_io_ctx *io_ctx = (struct virtio_scsi_io_ctx *)bdev_io->driver_ctx;
925 	int rc;
926 
927 	rc = virtqueue_req_start(ctrlq, bdev_io, 2);
928 	if (rc != 0) {
929 		return rc;
930 	}
931 
932 	virtqueue_req_add_iovs(ctrlq, &io_ctx->iov_req, 1, SPDK_VIRTIO_DESC_RO);
933 	virtqueue_req_add_iovs(ctrlq, &io_ctx->iov_resp, 1, SPDK_VIRTIO_DESC_WR);
934 
935 	virtqueue_req_flush(ctrlq);
936 	return 0;
937 }
938 
939 static int
940 bdev_virtio_mgmt_poll(void *arg)
941 {
942 	struct virtio_scsi_dev *svdev = arg;
943 	struct virtio_dev *vdev = &svdev->vdev;
944 	struct virtqueue *eventq = vdev->vqs[VIRTIO_SCSI_EVENTQ];
945 	struct virtqueue *ctrlq = vdev->vqs[VIRTIO_SCSI_CONTROLQ];
946 	struct spdk_ring *send_ring = svdev->ctrlq_ring;
947 	void *io[16];
948 	uint32_t io_len[16];
949 	uint16_t i, cnt;
950 	int rc;
951 	int total = 0;
952 
953 	cnt = spdk_ring_dequeue(send_ring, io, SPDK_COUNTOF(io));
954 	total += cnt;
955 	for (i = 0; i < cnt; ++i) {
956 		rc = bdev_virtio_send_tmf_io(ctrlq, io[i]);
957 		if (rc != 0) {
958 			bdev_virtio_tmf_abort(io[i], rc);
959 		}
960 	}
961 
962 	cnt = virtio_recv_pkts(ctrlq, io, io_len, SPDK_COUNTOF(io));
963 	total += cnt;
964 	for (i = 0; i < cnt; ++i) {
965 		bdev_virtio_tmf_cpl(io[i]);
966 	}
967 
968 	cnt = virtio_recv_pkts(eventq, io, io_len, SPDK_COUNTOF(io));
969 	total += cnt;
970 	for (i = 0; i < cnt; ++i) {
971 		bdev_virtio_eventq_io_cpl(svdev, io[i]);
972 	}
973 
974 	return total;
975 }
976 
977 static int
978 bdev_virtio_scsi_ch_create_cb(void *io_device, void *ctx_buf)
979 {
980 	struct virtio_scsi_dev *svdev = io_device;
981 	struct virtio_dev *vdev = &svdev->vdev;
982 	struct bdev_virtio_io_channel *ch = ctx_buf;
983 	struct virtqueue *vq;
984 	int32_t queue_idx;
985 
986 	queue_idx = virtio_dev_find_and_acquire_queue(vdev, VIRTIO_SCSI_REQUESTQ);
987 	if (queue_idx < 0) {
988 		SPDK_ERRLOG("Couldn't get an unused queue for the io_channel.\n");
989 		return -1;
990 	}
991 
992 	vq = vdev->vqs[queue_idx];
993 
994 	ch->svdev = svdev;
995 	ch->vq = vq;
996 
997 	ch->poller = SPDK_POLLER_REGISTER(bdev_virtio_poll, ch, 0);
998 
999 	return 0;
1000 }
1001 
1002 static void
1003 bdev_virtio_scsi_ch_destroy_cb(void *io_device, void *ctx_buf)
1004 {
1005 	struct bdev_virtio_io_channel *ch = ctx_buf;
1006 	struct virtio_scsi_dev *svdev = ch->svdev;
1007 	struct virtio_dev *vdev = &svdev->vdev;
1008 	struct virtqueue *vq = ch->vq;
1009 
1010 	spdk_poller_unregister(&ch->poller);
1011 	virtio_dev_release_queue(vdev, vq->vq_queue_index);
1012 }
1013 
1014 static void
1015 _virtio_scsi_dev_scan_finish(struct virtio_scsi_scan_base *base, int errnum)
1016 {
1017 	struct virtio_scsi_dev *svdev = base->svdev;
1018 	size_t bdevs_cnt;
1019 	struct spdk_bdev *bdevs[BDEV_VIRTIO_MAX_TARGET];
1020 	struct virtio_scsi_disk *disk;
1021 	struct virtio_scsi_scan_info *tgt, *next_tgt;
1022 
1023 	spdk_put_io_channel(spdk_io_channel_from_ctx(base->channel));
1024 	base->svdev->scan_ctx = NULL;
1025 
1026 	TAILQ_FOREACH_SAFE(tgt, &base->scan_queue, tailq, next_tgt) {
1027 		TAILQ_REMOVE(&base->scan_queue, tgt, tailq);
1028 		free(tgt);
1029 	}
1030 
1031 	if (base->cb_fn == NULL) {
1032 		spdk_free(base);
1033 		return;
1034 	}
1035 
1036 	bdevs_cnt = 0;
1037 	if (errnum == 0) {
1038 		TAILQ_FOREACH(disk, &svdev->luns, link) {
1039 			bdevs[bdevs_cnt] = &disk->bdev;
1040 			bdevs_cnt++;
1041 		}
1042 	}
1043 
1044 	base->cb_fn(base->cb_arg, errnum, bdevs, bdevs_cnt);
1045 	spdk_free(base);
1046 }
1047 
1048 static int
1049 send_scan_io(struct virtio_scsi_scan_base *base)
1050 {
1051 	struct virtio_scsi_io_ctx *io_ctx = &base->io_ctx;
1052 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1053 	struct virtqueue *vq = base->channel->vq;
1054 	int payload_iov_cnt = base->iov.iov_len > 0 ? 1 : 0;
1055 	int rc;
1056 
1057 	req->lun[0] = 1;
1058 	req->lun[1] = base->info.target;
1059 
1060 	rc = virtqueue_req_start(vq, io_ctx, 2 + payload_iov_cnt);
1061 	if (rc != 0) {
1062 		base->needs_resend = true;
1063 		return -1;
1064 	}
1065 
1066 	virtqueue_req_add_iovs(vq, &io_ctx->iov_req, 1, SPDK_VIRTIO_DESC_RO);
1067 	virtqueue_req_add_iovs(vq, &io_ctx->iov_resp, 1, SPDK_VIRTIO_DESC_WR);
1068 	virtqueue_req_add_iovs(vq, &base->iov, payload_iov_cnt, SPDK_VIRTIO_DESC_WR);
1069 
1070 	virtqueue_req_flush(vq);
1071 	return 0;
1072 }
1073 
1074 static int
1075 send_inquiry(struct virtio_scsi_scan_base *base)
1076 {
1077 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1078 	struct spdk_scsi_cdb_inquiry *cdb;
1079 
1080 	memset(req, 0, sizeof(*req));
1081 
1082 	base->iov.iov_len = BDEV_VIRTIO_SCAN_PAYLOAD_SIZE;
1083 	cdb = (struct spdk_scsi_cdb_inquiry *)req->cdb;
1084 	cdb->opcode = SPDK_SPC_INQUIRY;
1085 	to_be16(cdb->alloc_len, BDEV_VIRTIO_SCAN_PAYLOAD_SIZE);
1086 
1087 	return send_scan_io(base);
1088 }
1089 
1090 static int
1091 send_inquiry_vpd(struct virtio_scsi_scan_base *base, uint8_t page_code)
1092 {
1093 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1094 	struct spdk_scsi_cdb_inquiry *inquiry_cdb = (struct spdk_scsi_cdb_inquiry *)req->cdb;
1095 
1096 	memset(req, 0, sizeof(*req));
1097 
1098 	base->iov.iov_len = BDEV_VIRTIO_SCAN_PAYLOAD_SIZE;
1099 	inquiry_cdb->opcode = SPDK_SPC_INQUIRY;
1100 	inquiry_cdb->evpd = 1;
1101 	inquiry_cdb->page_code = page_code;
1102 	to_be16(inquiry_cdb->alloc_len, base->iov.iov_len);
1103 
1104 	return send_scan_io(base);
1105 }
1106 
1107 static int
1108 send_read_cap_10(struct virtio_scsi_scan_base *base)
1109 {
1110 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1111 
1112 	memset(req, 0, sizeof(*req));
1113 
1114 	base->iov.iov_len = 8;
1115 	req->cdb[0] = SPDK_SBC_READ_CAPACITY_10;
1116 
1117 	return send_scan_io(base);
1118 }
1119 
1120 static int
1121 send_read_cap_16(struct virtio_scsi_scan_base *base)
1122 {
1123 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1124 
1125 	memset(req, 0, sizeof(*req));
1126 
1127 	base->iov.iov_len = 32;
1128 	req->cdb[0] = SPDK_SPC_SERVICE_ACTION_IN_16;
1129 	req->cdb[1] = SPDK_SBC_SAI_READ_CAPACITY_16;
1130 	to_be32(&req->cdb[10], base->iov.iov_len);
1131 
1132 	return send_scan_io(base);
1133 }
1134 
1135 static int
1136 send_test_unit_ready(struct virtio_scsi_scan_base *base)
1137 {
1138 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1139 
1140 	memset(req, 0, sizeof(*req));
1141 	req->cdb[0] = SPDK_SPC_TEST_UNIT_READY;
1142 	base->iov.iov_len = 0;
1143 
1144 	return send_scan_io(base);
1145 }
1146 
1147 static int
1148 send_start_stop_unit(struct virtio_scsi_scan_base *base)
1149 {
1150 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1151 
1152 	memset(req, 0, sizeof(*req));
1153 	req->cdb[0] = SPDK_SBC_START_STOP_UNIT;
1154 	req->cdb[4] = SPDK_SBC_START_STOP_UNIT_START_BIT;
1155 	base->iov.iov_len = 0;
1156 
1157 	return send_scan_io(base);
1158 }
1159 
1160 static int
1161 process_scan_start_stop_unit(struct virtio_scsi_scan_base *base)
1162 {
1163 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1164 
1165 	if (resp->status == SPDK_SCSI_STATUS_GOOD) {
1166 		return send_inquiry_vpd(base, SPDK_SPC_VPD_SUPPORTED_VPD_PAGES);
1167 	}
1168 
1169 	return -1;
1170 }
1171 
1172 static int
1173 process_scan_test_unit_ready(struct virtio_scsi_scan_base *base)
1174 {
1175 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1176 	int sk, asc, ascq;
1177 
1178 	get_scsi_status(resp, &sk, &asc, &ascq);
1179 
1180 	/* check response, get VPD if spun up otherwise send SSU */
1181 	if (resp->status == SPDK_SCSI_STATUS_GOOD) {
1182 		return send_inquiry_vpd(base, SPDK_SPC_VPD_SUPPORTED_VPD_PAGES);
1183 	} else if (resp->response == VIRTIO_SCSI_S_OK &&
1184 		   resp->status == SPDK_SCSI_STATUS_CHECK_CONDITION &&
1185 		   sk == SPDK_SCSI_SENSE_UNIT_ATTENTION &&
1186 		   asc == SPDK_SCSI_ASC_LOGICAL_UNIT_NOT_READY) {
1187 		return send_start_stop_unit(base);
1188 	} else {
1189 		return -1;
1190 	}
1191 }
1192 
1193 static int
1194 process_scan_inquiry_standard(struct virtio_scsi_scan_base *base)
1195 {
1196 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1197 	struct spdk_scsi_cdb_inquiry_data *inquiry_data =
1198 		(struct spdk_scsi_cdb_inquiry_data *)base->payload;
1199 
1200 	if (resp->status != SPDK_SCSI_STATUS_GOOD) {
1201 		return -1;
1202 	}
1203 
1204 	/* check to make sure its a supported device */
1205 	if (inquiry_data->peripheral_device_type != SPDK_SPC_PERIPHERAL_DEVICE_TYPE_DISK ||
1206 	    inquiry_data->peripheral_qualifier != SPDK_SPC_PERIPHERAL_QUALIFIER_CONNECTED) {
1207 		SPDK_WARNLOG("Unsupported peripheral device type 0x%02x (qualifier 0x%02x)\n",
1208 			     inquiry_data->peripheral_device_type,
1209 			     inquiry_data->peripheral_qualifier);
1210 		return -1;
1211 	}
1212 
1213 	return send_test_unit_ready(base);
1214 }
1215 
1216 static int
1217 process_scan_inquiry_vpd_supported_vpd_pages(struct virtio_scsi_scan_base *base)
1218 {
1219 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1220 	bool block_provisioning_page_supported = false;
1221 
1222 	if (resp->status == SPDK_SCSI_STATUS_GOOD) {
1223 		const uint8_t *vpd_data = base->payload;
1224 		const uint8_t *supported_vpd_pages = vpd_data + 4;
1225 		uint16_t page_length;
1226 		uint16_t num_supported_pages;
1227 		uint16_t i;
1228 
1229 		page_length = from_be16(vpd_data + 2);
1230 		num_supported_pages = spdk_min(page_length, base->iov.iov_len - 4);
1231 
1232 		for (i = 0; i < num_supported_pages; i++) {
1233 			if (supported_vpd_pages[i] == SPDK_SPC_VPD_BLOCK_THIN_PROVISION) {
1234 				block_provisioning_page_supported = true;
1235 				break;
1236 			}
1237 		}
1238 	}
1239 
1240 	if (block_provisioning_page_supported) {
1241 		return send_inquiry_vpd(base, SPDK_SPC_VPD_BLOCK_THIN_PROVISION);
1242 	} else {
1243 		return send_read_cap_10(base);
1244 	}
1245 }
1246 
1247 static int
1248 process_scan_inquiry_vpd_block_thin_provision(struct virtio_scsi_scan_base *base)
1249 {
1250 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1251 
1252 	base->info.unmap_supported = false;
1253 
1254 	if (resp->status == SPDK_SCSI_STATUS_GOOD) {
1255 		uint8_t *vpd_data = base->payload;
1256 
1257 		base->info.unmap_supported = !!(vpd_data[5] & SPDK_SCSI_UNMAP_LBPU);
1258 	}
1259 
1260 	SPDK_INFOLOG(virtio, "Target %u: unmap supported = %d\n",
1261 		     base->info.target, (int)base->info.unmap_supported);
1262 
1263 	return send_read_cap_10(base);
1264 }
1265 
1266 static int
1267 process_scan_inquiry(struct virtio_scsi_scan_base *base)
1268 {
1269 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1270 	struct spdk_scsi_cdb_inquiry *inquiry_cdb = (struct spdk_scsi_cdb_inquiry *)req->cdb;
1271 
1272 	if ((inquiry_cdb->evpd & 1) == 0) {
1273 		return process_scan_inquiry_standard(base);
1274 	}
1275 
1276 	switch (inquiry_cdb->page_code) {
1277 	case SPDK_SPC_VPD_SUPPORTED_VPD_PAGES:
1278 		return process_scan_inquiry_vpd_supported_vpd_pages(base);
1279 	case SPDK_SPC_VPD_BLOCK_THIN_PROVISION:
1280 		return process_scan_inquiry_vpd_block_thin_provision(base);
1281 	default:
1282 		SPDK_DEBUGLOG(virtio, "Unexpected VPD page 0x%02x\n", inquiry_cdb->page_code);
1283 		return -1;
1284 	}
1285 }
1286 
1287 static void
1288 bdev_virtio_disk_notify_remove(struct virtio_scsi_disk *disk)
1289 {
1290 	disk->removed = true;
1291 	spdk_bdev_close(disk->notify_desc);
1292 }
1293 
1294 static void
1295 bdev_virtio_disk_notify_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
1296 				 void *event_ctx)
1297 {
1298 	switch (type) {
1299 	case SPDK_BDEV_EVENT_REMOVE:
1300 		bdev_virtio_disk_notify_remove(event_ctx);
1301 		break;
1302 	default:
1303 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
1304 		break;
1305 	}
1306 }
1307 
1308 /* To be called only from the thread performing target scan */
1309 static int
1310 virtio_scsi_dev_add_tgt(struct virtio_scsi_dev *svdev, struct virtio_scsi_scan_info *info)
1311 {
1312 	struct virtio_scsi_disk *disk;
1313 	struct spdk_bdev *bdev;
1314 	int rc;
1315 
1316 	TAILQ_FOREACH(disk, &svdev->luns, link) {
1317 		if (disk->info.target == info->target) {
1318 			/* Target is already attached and param change is not supported */
1319 			return 0;
1320 		}
1321 	}
1322 
1323 	if (info->block_size == 0 || info->num_blocks == 0) {
1324 		SPDK_ERRLOG("%s: invalid target %u: bs=%"PRIu32" blocks=%"PRIu64"\n",
1325 			    svdev->vdev.name, info->target, info->block_size, info->num_blocks);
1326 		return -EINVAL;
1327 	}
1328 
1329 	disk = calloc(1, sizeof(*disk));
1330 	if (disk == NULL) {
1331 		SPDK_ERRLOG("could not allocate disk\n");
1332 		return -ENOMEM;
1333 	}
1334 
1335 	disk->svdev = svdev;
1336 	memcpy(&disk->info, info, sizeof(*info));
1337 
1338 	bdev = &disk->bdev;
1339 	bdev->name = spdk_sprintf_alloc("%st%"PRIu8, svdev->vdev.name, info->target);
1340 	if (bdev->name == NULL) {
1341 		SPDK_ERRLOG("Couldn't alloc memory for the bdev name.\n");
1342 		free(disk);
1343 		return -ENOMEM;
1344 	}
1345 
1346 	bdev->product_name = "Virtio SCSI Disk";
1347 	bdev->write_cache = 0;
1348 	bdev->blocklen = disk->info.block_size;
1349 	bdev->blockcnt = disk->info.num_blocks;
1350 
1351 	bdev->ctxt = disk;
1352 	bdev->fn_table = &virtio_fn_table;
1353 	bdev->module = &virtio_scsi_if;
1354 
1355 	rc = spdk_bdev_register(&disk->bdev);
1356 	if (rc) {
1357 		SPDK_ERRLOG("Failed to register bdev name=%s\n", disk->bdev.name);
1358 		free(bdev->name);
1359 		free(disk);
1360 		return rc;
1361 	}
1362 
1363 	rc = spdk_bdev_open_ext(bdev->name, false, bdev_virtio_disk_notify_event_cb,
1364 				disk, &disk->notify_desc);
1365 	if (rc) {
1366 		assert(false);
1367 	}
1368 
1369 	TAILQ_INSERT_TAIL(&svdev->luns, disk, link);
1370 	return 0;
1371 }
1372 
1373 static int
1374 process_read_cap_10(struct virtio_scsi_scan_base *base)
1375 {
1376 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1377 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1378 	uint64_t max_block;
1379 	uint32_t block_size;
1380 	uint8_t target_id = req->lun[1];
1381 	int rc;
1382 
1383 	if (resp->response != VIRTIO_SCSI_S_OK || resp->status != SPDK_SCSI_STATUS_GOOD) {
1384 		SPDK_ERRLOG("READ CAPACITY (10) failed for target %"PRIu8".\n", target_id);
1385 		return -1;
1386 	}
1387 
1388 	block_size = from_be32(base->payload + 4);
1389 	max_block = from_be32(base->payload);
1390 
1391 	if (max_block == 0xffffffff) {
1392 		return send_read_cap_16(base);
1393 	}
1394 
1395 	base->info.num_blocks = (uint64_t)max_block + 1;
1396 	base->info.block_size = block_size;
1397 
1398 	rc = virtio_scsi_dev_add_tgt(base->svdev, &base->info);
1399 	if (rc != 0) {
1400 		return rc;
1401 	}
1402 
1403 	return _virtio_scsi_dev_scan_next(base, 0);
1404 }
1405 
1406 static int
1407 process_read_cap_16(struct virtio_scsi_scan_base *base)
1408 {
1409 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1410 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1411 	uint8_t target_id = req->lun[1];
1412 	int rc;
1413 
1414 	if (resp->response != VIRTIO_SCSI_S_OK || resp->status != SPDK_SCSI_STATUS_GOOD) {
1415 		SPDK_ERRLOG("READ CAPACITY (16) failed for target %"PRIu8".\n", target_id);
1416 		return -1;
1417 	}
1418 
1419 	base->info.num_blocks = from_be64(base->payload) + 1;
1420 	base->info.block_size = from_be32(base->payload + 8);
1421 	rc = virtio_scsi_dev_add_tgt(base->svdev, &base->info);
1422 	if (rc != 0) {
1423 		return rc;
1424 	}
1425 
1426 	return _virtio_scsi_dev_scan_next(base, 0);
1427 }
1428 
1429 static void
1430 process_scan_resp(struct virtio_scsi_scan_base *base)
1431 {
1432 	struct virtio_scsi_cmd_req *req = &base->io_ctx.req;
1433 	struct virtio_scsi_cmd_resp *resp = &base->io_ctx.resp;
1434 	int rc, sk, asc, ascq;
1435 	uint8_t target_id;
1436 
1437 	if (base->io_ctx.iov_req.iov_len < sizeof(struct virtio_scsi_cmd_req) ||
1438 	    base->io_ctx.iov_resp.iov_len < sizeof(struct virtio_scsi_cmd_resp)) {
1439 		SPDK_ERRLOG("Received target scan message with invalid length.\n");
1440 		_virtio_scsi_dev_scan_next(base, -EIO);
1441 		return;
1442 	}
1443 
1444 	get_scsi_status(resp, &sk, &asc, &ascq);
1445 	target_id = req->lun[1];
1446 
1447 	if (resp->response == VIRTIO_SCSI_S_BAD_TARGET ||
1448 	    resp->response == VIRTIO_SCSI_S_INCORRECT_LUN) {
1449 		_virtio_scsi_dev_scan_next(base, -ENODEV);
1450 		return;
1451 	}
1452 
1453 	if (resp->response != VIRTIO_SCSI_S_OK ||
1454 	    (resp->status == SPDK_SCSI_STATUS_CHECK_CONDITION &&
1455 	     sk != SPDK_SCSI_SENSE_ILLEGAL_REQUEST)) {
1456 		assert(base->retries > 0);
1457 		base->retries--;
1458 		if (base->retries == 0) {
1459 			SPDK_NOTICELOG("Target %"PRIu8" is present, but unavailable.\n", target_id);
1460 			SPDK_LOGDUMP(virtio, "CDB", req->cdb, sizeof(req->cdb));
1461 			SPDK_LOGDUMP(virtio, "SENSE DATA", resp->sense, sizeof(resp->sense));
1462 			_virtio_scsi_dev_scan_next(base, -EBUSY);
1463 			return;
1464 		}
1465 
1466 		/* resend the same request */
1467 		rc = send_scan_io(base);
1468 		if (rc != 0) {
1469 			/* Let response poller do the resend */
1470 		}
1471 		return;
1472 	}
1473 
1474 	base->retries = SCAN_REQUEST_RETRIES;
1475 
1476 	switch (req->cdb[0]) {
1477 	case SPDK_SPC_INQUIRY:
1478 		rc = process_scan_inquiry(base);
1479 		break;
1480 	case SPDK_SPC_TEST_UNIT_READY:
1481 		rc = process_scan_test_unit_ready(base);
1482 		break;
1483 	case SPDK_SBC_START_STOP_UNIT:
1484 		rc = process_scan_start_stop_unit(base);
1485 		break;
1486 	case SPDK_SBC_READ_CAPACITY_10:
1487 		rc = process_read_cap_10(base);
1488 		break;
1489 	case SPDK_SPC_SERVICE_ACTION_IN_16:
1490 		rc = process_read_cap_16(base);
1491 		break;
1492 	default:
1493 		SPDK_ERRLOG("Received invalid target scan message: cdb[0] = %"PRIu8".\n", req->cdb[0]);
1494 		rc = -1;
1495 		break;
1496 	}
1497 
1498 	if (rc != 0) {
1499 		if (base->needs_resend) {
1500 			return; /* Let response poller do the resend */
1501 		}
1502 
1503 		_virtio_scsi_dev_scan_next(base, rc);
1504 	}
1505 }
1506 
1507 static int
1508 _virtio_scsi_dev_scan_next(struct virtio_scsi_scan_base *base, int rc)
1509 {
1510 	struct virtio_scsi_scan_info *next;
1511 	struct virtio_scsi_disk *disk;
1512 	uint8_t target_id;
1513 
1514 	if (base->full_scan) {
1515 		if (rc != 0) {
1516 			disk = virtio_scsi_dev_get_disk_by_id(base->svdev,
1517 							      base->info.target);
1518 			if (disk != NULL) {
1519 				spdk_bdev_unregister(&disk->bdev, NULL, NULL);
1520 			}
1521 		}
1522 
1523 		target_id = base->info.target + 1;
1524 		if (target_id < BDEV_VIRTIO_MAX_TARGET) {
1525 			_virtio_scsi_dev_scan_tgt(base, target_id);
1526 			return 0;
1527 		}
1528 
1529 		base->full_scan = false;
1530 	}
1531 
1532 	next = TAILQ_FIRST(&base->scan_queue);
1533 	if (next == NULL) {
1534 		_virtio_scsi_dev_scan_finish(base, 0);
1535 		return 0;
1536 	}
1537 
1538 	TAILQ_REMOVE(&base->scan_queue, next, tailq);
1539 	target_id = next->target;
1540 	free(next);
1541 
1542 	_virtio_scsi_dev_scan_tgt(base, target_id);
1543 	return 0;
1544 }
1545 
1546 static int
1547 _virtio_scsi_dev_scan_init(struct virtio_scsi_dev *svdev)
1548 {
1549 	struct virtio_scsi_scan_base *base;
1550 	struct spdk_io_channel *io_ch;
1551 	struct virtio_scsi_io_ctx *io_ctx;
1552 	struct virtio_scsi_cmd_req *req;
1553 	struct virtio_scsi_cmd_resp *resp;
1554 
1555 	io_ch = spdk_get_io_channel(svdev);
1556 	if (io_ch == NULL) {
1557 		return -EBUSY;
1558 	}
1559 
1560 	base = spdk_zmalloc(sizeof(*base), 64, NULL,
1561 			    SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
1562 	if (base == NULL) {
1563 		SPDK_ERRLOG("couldn't allocate memory for scsi target scan.\n");
1564 		return -ENOMEM;
1565 	}
1566 
1567 	base->svdev = svdev;
1568 
1569 	base->channel = spdk_io_channel_get_ctx(io_ch);
1570 	TAILQ_INIT(&base->scan_queue);
1571 	svdev->scan_ctx = base;
1572 
1573 	base->iov.iov_base = base->payload;
1574 	io_ctx = &base->io_ctx;
1575 	req = &io_ctx->req;
1576 	resp = &io_ctx->resp;
1577 	io_ctx->iov_req.iov_base = req;
1578 	io_ctx->iov_req.iov_len = sizeof(*req);
1579 	io_ctx->iov_resp.iov_base = resp;
1580 	io_ctx->iov_resp.iov_len = sizeof(*resp);
1581 
1582 	base->retries = SCAN_REQUEST_RETRIES;
1583 	return 0;
1584 }
1585 
1586 static void
1587 _virtio_scsi_dev_scan_tgt(struct virtio_scsi_scan_base *base, uint8_t target)
1588 {
1589 	int rc;
1590 
1591 	memset(&base->info, 0, sizeof(base->info));
1592 	base->info.target = target;
1593 
1594 	rc = send_inquiry(base);
1595 	if (rc) {
1596 		/* Let response poller do the resend */
1597 	}
1598 }
1599 
1600 static int
1601 virtio_scsi_dev_scan(struct virtio_scsi_dev *svdev, bdev_virtio_create_cb cb_fn,
1602 		     void *cb_arg)
1603 {
1604 	struct virtio_scsi_scan_base *base;
1605 	struct virtio_scsi_scan_info *tgt, *next_tgt;
1606 	int rc;
1607 
1608 	if (svdev->scan_ctx) {
1609 		if (svdev->scan_ctx->full_scan) {
1610 			return -EEXIST;
1611 		}
1612 
1613 		/* We're about to start a full rescan, so there's no need
1614 		 * to scan particular targets afterwards.
1615 		 */
1616 		TAILQ_FOREACH_SAFE(tgt, &svdev->scan_ctx->scan_queue, tailq, next_tgt) {
1617 			TAILQ_REMOVE(&svdev->scan_ctx->scan_queue, tgt, tailq);
1618 			free(tgt);
1619 		}
1620 
1621 		svdev->scan_ctx->cb_fn = cb_fn;
1622 		svdev->scan_ctx->cb_arg = cb_arg;
1623 		svdev->scan_ctx->restart = true;
1624 		return 0;
1625 	}
1626 
1627 	rc = _virtio_scsi_dev_scan_init(svdev);
1628 	if (rc != 0) {
1629 		return rc;
1630 	}
1631 
1632 	base = svdev->scan_ctx;
1633 	base->cb_fn = cb_fn;
1634 	base->cb_arg = cb_arg;
1635 	base->full_scan = true;
1636 
1637 	_virtio_scsi_dev_scan_tgt(base, 0);
1638 	return 0;
1639 }
1640 
1641 static int
1642 virtio_scsi_dev_scan_tgt(struct virtio_scsi_dev *svdev, uint8_t target)
1643 {
1644 	struct virtio_scsi_scan_base *base;
1645 	struct virtio_scsi_scan_info *info;
1646 	int rc;
1647 
1648 	base = svdev->scan_ctx;
1649 	if (base) {
1650 		info = calloc(1, sizeof(*info));
1651 		if (info == NULL) {
1652 			SPDK_ERRLOG("calloc failed\n");
1653 			return -ENOMEM;
1654 		}
1655 
1656 		info->target = target;
1657 		TAILQ_INSERT_TAIL(&base->scan_queue, info, tailq);
1658 		return 0;
1659 	}
1660 
1661 	rc = _virtio_scsi_dev_scan_init(svdev);
1662 	if (rc != 0) {
1663 		return rc;
1664 	}
1665 
1666 	base = svdev->scan_ctx;
1667 	base->full_scan = true;
1668 	_virtio_scsi_dev_scan_tgt(base, target);
1669 	return 0;
1670 }
1671 
1672 static int
1673 bdev_virtio_initialize(void)
1674 {
1675 	return 0;
1676 }
1677 
1678 static void
1679 _virtio_scsi_dev_unregister_cb(void *io_device)
1680 {
1681 	struct virtio_scsi_dev *svdev = io_device;
1682 	struct virtio_dev *vdev = &svdev->vdev;
1683 	bool finish_module;
1684 	bdev_virtio_remove_cb remove_cb;
1685 	void *remove_ctx;
1686 
1687 	assert(spdk_ring_count(svdev->ctrlq_ring) == 0);
1688 	spdk_ring_free(svdev->ctrlq_ring);
1689 	spdk_poller_unregister(&svdev->mgmt_poller);
1690 
1691 	virtio_dev_release_queue(vdev, VIRTIO_SCSI_EVENTQ);
1692 	virtio_dev_release_queue(vdev, VIRTIO_SCSI_CONTROLQ);
1693 
1694 	virtio_dev_stop(vdev);
1695 	virtio_dev_destruct(vdev);
1696 
1697 	pthread_mutex_lock(&g_virtio_scsi_mutex);
1698 	TAILQ_REMOVE(&g_virtio_scsi_devs, svdev, tailq);
1699 	pthread_mutex_unlock(&g_virtio_scsi_mutex);
1700 
1701 	remove_cb = svdev->remove_cb;
1702 	remove_ctx = svdev->remove_ctx;
1703 	spdk_free(svdev->eventq_ios);
1704 	free(svdev);
1705 
1706 	if (remove_cb) {
1707 		remove_cb(remove_ctx, 0);
1708 	}
1709 
1710 	finish_module = TAILQ_EMPTY(&g_virtio_scsi_devs);
1711 
1712 	if (g_bdev_virtio_finish && finish_module) {
1713 		spdk_bdev_module_fini_done();
1714 	}
1715 }
1716 
1717 static void
1718 virtio_scsi_dev_unregister_cb(void *io_device)
1719 {
1720 	struct virtio_scsi_dev *svdev = io_device;
1721 	struct spdk_thread *thread;
1722 
1723 	thread = virtio_dev_queue_get_thread(&svdev->vdev, VIRTIO_SCSI_CONTROLQ);
1724 	spdk_thread_send_msg(thread, _virtio_scsi_dev_unregister_cb, io_device);
1725 }
1726 
1727 static void
1728 virtio_scsi_dev_remove(struct virtio_scsi_dev *svdev,
1729 		       bdev_virtio_remove_cb cb_fn, void *cb_arg)
1730 {
1731 	struct virtio_scsi_disk *disk, *disk_tmp;
1732 	bool do_remove = true;
1733 
1734 	if (svdev->removed) {
1735 		if (cb_fn) {
1736 			cb_fn(cb_arg, -EBUSY);
1737 		}
1738 		return;
1739 	}
1740 
1741 	svdev->remove_cb = cb_fn;
1742 	svdev->remove_ctx = cb_arg;
1743 	svdev->removed = true;
1744 
1745 	if (svdev->scan_ctx) {
1746 		/* The removal will continue after we receive a pending scan I/O. */
1747 		return;
1748 	}
1749 
1750 	TAILQ_FOREACH_SAFE(disk, &svdev->luns, link, disk_tmp) {
1751 		if (!disk->removed) {
1752 			spdk_bdev_unregister(&disk->bdev, NULL, NULL);
1753 		}
1754 		do_remove = false;
1755 	}
1756 
1757 	if (do_remove) {
1758 		spdk_io_device_unregister(svdev, virtio_scsi_dev_unregister_cb);
1759 	}
1760 }
1761 
1762 static void
1763 bdev_virtio_finish(void)
1764 {
1765 	struct virtio_scsi_dev *svdev, *next;
1766 
1767 	g_bdev_virtio_finish = true;
1768 
1769 	pthread_mutex_lock(&g_virtio_scsi_mutex);
1770 	if (TAILQ_EMPTY(&g_virtio_scsi_devs)) {
1771 		pthread_mutex_unlock(&g_virtio_scsi_mutex);
1772 		spdk_bdev_module_fini_done();
1773 		return;
1774 	}
1775 
1776 	/* Defer module finish until all controllers are removed. */
1777 	TAILQ_FOREACH_SAFE(svdev, &g_virtio_scsi_devs, tailq, next) {
1778 		virtio_scsi_dev_remove(svdev, NULL, NULL);
1779 	}
1780 	pthread_mutex_unlock(&g_virtio_scsi_mutex);
1781 }
1782 
1783 int
1784 bdev_virtio_user_scsi_dev_create(const char *base_name, const char *path,
1785 				 unsigned num_queues, unsigned queue_size,
1786 				 bdev_virtio_create_cb cb_fn, void *cb_arg)
1787 {
1788 	struct virtio_scsi_dev *svdev;
1789 	int rc;
1790 
1791 	svdev = virtio_user_scsi_dev_create(base_name, path, num_queues, queue_size);
1792 	if (svdev == NULL) {
1793 		return -1;
1794 	}
1795 
1796 	rc = virtio_scsi_dev_scan(svdev, cb_fn, cb_arg);
1797 	if (rc) {
1798 		virtio_scsi_dev_remove(svdev, NULL, NULL);
1799 	}
1800 
1801 	return rc;
1802 }
1803 
1804 struct bdev_virtio_pci_dev_create_ctx {
1805 	const char *name;
1806 	bdev_virtio_create_cb cb_fn;
1807 	void *cb_arg;
1808 };
1809 
1810 static int
1811 bdev_virtio_pci_scsi_dev_create_cb(struct virtio_pci_ctx *pci_ctx, void *ctx)
1812 {
1813 	struct virtio_scsi_dev *svdev;
1814 	struct bdev_virtio_pci_dev_create_ctx *create_ctx = ctx;
1815 	int rc;
1816 
1817 	svdev = virtio_pci_scsi_dev_create(create_ctx->name, pci_ctx);
1818 	if (svdev == NULL) {
1819 		return -1;
1820 	}
1821 
1822 	rc = virtio_scsi_dev_scan(svdev, create_ctx->cb_fn, create_ctx->cb_arg);
1823 	if (rc) {
1824 		svdev->vdev.ctx = NULL;
1825 		virtio_scsi_dev_remove(svdev, NULL, NULL);
1826 	}
1827 
1828 	return rc;
1829 }
1830 
1831 int
1832 bdev_virtio_pci_scsi_dev_create(const char *name, struct spdk_pci_addr *pci_addr,
1833 				bdev_virtio_create_cb cb_fn, void *cb_arg)
1834 {
1835 	struct bdev_virtio_pci_dev_create_ctx create_ctx;
1836 
1837 	create_ctx.name = name;
1838 	create_ctx.cb_fn = cb_fn;
1839 	create_ctx.cb_arg = cb_arg;
1840 
1841 	return virtio_pci_dev_attach(bdev_virtio_pci_scsi_dev_create_cb, &create_ctx,
1842 				     VIRTIO_ID_SCSI, pci_addr);
1843 }
1844 
1845 int
1846 bdev_virtio_scsi_dev_remove(const char *name, bdev_virtio_remove_cb cb_fn, void *cb_arg)
1847 {
1848 	struct virtio_scsi_dev *svdev;
1849 
1850 	pthread_mutex_lock(&g_virtio_scsi_mutex);
1851 	TAILQ_FOREACH(svdev, &g_virtio_scsi_devs, tailq) {
1852 		if (strcmp(svdev->vdev.name, name) == 0) {
1853 			break;
1854 		}
1855 	}
1856 
1857 	if (svdev == NULL) {
1858 		pthread_mutex_unlock(&g_virtio_scsi_mutex);
1859 		SPDK_ERRLOG("Cannot find Virtio-SCSI device named '%s'\n", name);
1860 		return -ENODEV;
1861 	}
1862 
1863 	virtio_scsi_dev_remove(svdev, cb_fn, cb_arg);
1864 	pthread_mutex_unlock(&g_virtio_scsi_mutex);
1865 
1866 	return 0;
1867 }
1868 
1869 void
1870 bdev_virtio_scsi_dev_list(struct spdk_json_write_ctx *w)
1871 {
1872 	struct virtio_scsi_dev *svdev;
1873 
1874 	spdk_json_write_array_begin(w);
1875 
1876 	pthread_mutex_lock(&g_virtio_scsi_mutex);
1877 	TAILQ_FOREACH(svdev, &g_virtio_scsi_devs, tailq) {
1878 		spdk_json_write_object_begin(w);
1879 
1880 		spdk_json_write_named_string(w, "name", svdev->vdev.name);
1881 
1882 		virtio_dev_dump_json_info(&svdev->vdev, w);
1883 
1884 		spdk_json_write_object_end(w);
1885 	}
1886 	pthread_mutex_unlock(&g_virtio_scsi_mutex);
1887 
1888 	spdk_json_write_array_end(w);
1889 }
1890 
1891 SPDK_LOG_REGISTER_COMPONENT(virtio)
1892