xref: /spdk/lib/vhost/vhost_scsi.c (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 Intel Corporation. All rights reserved.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include <linux/virtio_scsi.h>
9 
10 #include "spdk/env.h"
11 #include "spdk/thread.h"
12 #include "spdk/scsi.h"
13 #include "spdk/scsi_spec.h"
14 #include "spdk/util.h"
15 #include "spdk/likely.h"
16 
17 #include "spdk/vhost.h"
18 #include "vhost_internal.h"
19 
20 /* Features supported by SPDK VHOST lib. */
21 #define SPDK_VHOST_SCSI_FEATURES	(SPDK_VHOST_FEATURES | \
22 					(1ULL << VIRTIO_SCSI_F_INOUT) | \
23 					(1ULL << VIRTIO_SCSI_F_HOTPLUG) | \
24 					(1ULL << VIRTIO_SCSI_F_CHANGE ) | \
25 					(1ULL << VIRTIO_SCSI_F_T10_PI ))
26 
27 /* Features that are specified in VIRTIO SCSI but currently not supported:
28  * - Live migration not supported yet
29  * - T10 PI
30  */
31 #define SPDK_VHOST_SCSI_DISABLED_FEATURES	(SPDK_VHOST_DISABLED_FEATURES | \
32 						(1ULL << VIRTIO_SCSI_F_T10_PI ))
33 
34 /* Vhost-user-scsi support protocol features */
35 #define SPDK_VHOST_SCSI_PROTOCOL_FEATURES	(1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)
36 
37 #define MGMT_POLL_PERIOD_US (1000 * 5)
38 
39 #define VIRTIO_SCSI_CONTROLQ   0
40 #define VIRTIO_SCSI_EVENTQ   1
41 #define VIRTIO_SCSI_REQUESTQ   2
42 
43 enum spdk_scsi_dev_vhost_status {
44 	/* Target ID is empty. */
45 	VHOST_SCSI_DEV_EMPTY,
46 
47 	/* Target is still being added. */
48 	VHOST_SCSI_DEV_ADDING,
49 
50 	/* Target ID occupied. */
51 	VHOST_SCSI_DEV_PRESENT,
52 
53 	/* Target ID is occupied but removal is in progress. */
54 	VHOST_SCSI_DEV_REMOVING,
55 
56 	/* In session - device (SCSI target) seen but removed. */
57 	VHOST_SCSI_DEV_REMOVED,
58 };
59 
60 /** Context for a SCSI target in a vhost device */
61 struct spdk_scsi_dev_vhost_state {
62 	struct spdk_scsi_dev *dev;
63 	enum spdk_scsi_dev_vhost_status status;
64 	spdk_vhost_event_fn remove_cb;
65 	void *remove_ctx;
66 };
67 
68 struct spdk_vhost_scsi_dev {
69 	int ref;
70 	bool registered;
71 	struct spdk_vhost_dev vdev;
72 	struct spdk_scsi_dev_vhost_state scsi_dev_state[SPDK_VHOST_SCSI_CTRLR_MAX_DEVS];
73 };
74 
75 /** Context for a SCSI target in a vhost session */
76 struct spdk_scsi_dev_session_state {
77 	struct spdk_scsi_dev *dev;
78 	enum spdk_scsi_dev_vhost_status status;
79 };
80 
81 struct spdk_vhost_scsi_session {
82 	struct spdk_vhost_session vsession;
83 
84 	struct spdk_vhost_scsi_dev *svdev;
85 	/** Local copy of the device state */
86 	struct spdk_scsi_dev_session_state scsi_dev_state[SPDK_VHOST_SCSI_CTRLR_MAX_DEVS];
87 	struct spdk_poller *requestq_poller;
88 	struct spdk_poller *mgmt_poller;
89 	struct spdk_poller *stop_poller;
90 };
91 
92 struct spdk_vhost_scsi_task {
93 	struct spdk_scsi_task	scsi;
94 	struct iovec iovs[SPDK_VHOST_IOVS_MAX];
95 
96 	union {
97 		struct virtio_scsi_cmd_resp *resp;
98 		struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
99 	};
100 
101 	struct spdk_vhost_scsi_session *svsession;
102 	struct spdk_scsi_dev *scsi_dev;
103 
104 	/** Number of bytes that were written. */
105 	uint32_t used_len;
106 
107 	int req_idx;
108 
109 	/* If set, the task is currently used for I/O processing. */
110 	bool used;
111 
112 	struct spdk_vhost_virtqueue *vq;
113 };
114 
115 static int vhost_scsi_start(struct spdk_vhost_dev *vdev,
116 			    struct spdk_vhost_session *vsession, void *unused);
117 static int vhost_scsi_stop(struct spdk_vhost_session *vsession);
118 static void vhost_scsi_dump_info_json(struct spdk_vhost_dev *vdev,
119 				      struct spdk_json_write_ctx *w);
120 static void vhost_scsi_write_config_json(struct spdk_vhost_dev *vdev,
121 		struct spdk_json_write_ctx *w);
122 static int vhost_scsi_dev_remove(struct spdk_vhost_dev *vdev);
123 static int vhost_scsi_dev_param_changed(struct spdk_vhost_dev *vdev,
124 					unsigned scsi_tgt_num);
125 static int alloc_vq_task_pool(struct spdk_vhost_session *vsession, uint16_t qid);
126 
127 static const struct spdk_vhost_user_dev_backend spdk_vhost_scsi_user_device_backend = {
128 	.session_ctx_size = sizeof(struct spdk_vhost_scsi_session) - sizeof(struct spdk_vhost_session),
129 	.start_session =  vhost_scsi_start,
130 	.stop_session = vhost_scsi_stop,
131 	.alloc_vq_tasks = alloc_vq_task_pool,
132 };
133 
134 static const struct spdk_vhost_dev_backend spdk_vhost_scsi_device_backend = {
135 	.type = VHOST_BACKEND_SCSI,
136 	.dump_info_json = vhost_scsi_dump_info_json,
137 	.write_config_json = vhost_scsi_write_config_json,
138 	.remove_device = vhost_scsi_dev_remove,
139 };
140 
141 static inline void
142 scsi_task_init(struct spdk_vhost_scsi_task *task)
143 {
144 	memset(&task->scsi, 0, sizeof(task->scsi));
145 	/* Tmf_resp pointer and resp pointer are in a union.
146 	 * Here means task->tmf_resp = task->resp = NULL.
147 	 */
148 	task->resp = NULL;
149 	task->used = true;
150 	task->used_len = 0;
151 }
152 
153 static void
154 vhost_scsi_task_put(struct spdk_vhost_scsi_task *task)
155 {
156 	spdk_scsi_task_put(&task->scsi);
157 }
158 
159 static void
160 vhost_scsi_task_free_cb(struct spdk_scsi_task *scsi_task)
161 {
162 	struct spdk_vhost_scsi_task *task = SPDK_CONTAINEROF(scsi_task, struct spdk_vhost_scsi_task, scsi);
163 	struct spdk_vhost_session *vsession = &task->svsession->vsession;
164 
165 	assert(vsession->task_cnt > 0);
166 	vsession->task_cnt--;
167 	task->used = false;
168 }
169 
170 static void
171 remove_scsi_tgt(struct spdk_vhost_scsi_dev *svdev,
172 		unsigned scsi_tgt_num)
173 {
174 	struct spdk_scsi_dev_vhost_state *state;
175 	struct spdk_scsi_dev *dev;
176 
177 	state = &svdev->scsi_dev_state[scsi_tgt_num];
178 	dev = state->dev;
179 	state->dev = NULL;
180 	assert(state->status == VHOST_SCSI_DEV_REMOVING);
181 	state->status = VHOST_SCSI_DEV_EMPTY;
182 	spdk_scsi_dev_destruct(dev, NULL, NULL);
183 	if (state->remove_cb) {
184 		state->remove_cb(&svdev->vdev, state->remove_ctx);
185 		state->remove_cb = NULL;
186 	}
187 	SPDK_INFOLOG(vhost, "removed target 'Target %u'\n", scsi_tgt_num);
188 
189 	if (--svdev->ref == 0 && svdev->registered == false) {
190 		free(svdev);
191 	}
192 }
193 
194 static void
195 vhost_scsi_dev_process_removed_cpl_cb(struct spdk_vhost_dev *vdev, void *ctx)
196 {
197 	unsigned scsi_tgt_num = (unsigned)(uintptr_t)ctx;
198 	struct spdk_vhost_scsi_dev *svdev = SPDK_CONTAINEROF(vdev,
199 					    struct spdk_vhost_scsi_dev, vdev);
200 
201 	/* all sessions have already detached the device */
202 	if (svdev->scsi_dev_state[scsi_tgt_num].status != VHOST_SCSI_DEV_REMOVING) {
203 		/* device was already removed in the meantime */
204 		return;
205 	}
206 
207 	remove_scsi_tgt(svdev, scsi_tgt_num);
208 }
209 
210 static int
211 vhost_scsi_session_process_removed(struct spdk_vhost_dev *vdev,
212 				   struct spdk_vhost_session *vsession, void *ctx)
213 {
214 	unsigned scsi_tgt_num = (unsigned)(uintptr_t)ctx;
215 	struct spdk_vhost_scsi_session *svsession = (struct spdk_vhost_scsi_session *)vsession;
216 	struct spdk_scsi_dev_session_state *state = &svsession->scsi_dev_state[scsi_tgt_num];
217 
218 	if (state->dev != NULL) {
219 		/* there's still a session that references this device,
220 		 * so abort our foreach chain here. We'll be called
221 		 * again from this session's management poller after it
222 		 * is removed in there
223 		 */
224 		return -1;
225 	}
226 
227 	return 0;
228 }
229 
230 static void
231 process_removed_devs(struct spdk_vhost_scsi_session *svsession)
232 {
233 	struct spdk_scsi_dev *dev;
234 	struct spdk_scsi_dev_session_state *state;
235 	int i;
236 
237 	for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; ++i) {
238 		state = &svsession->scsi_dev_state[i];
239 		dev = state->dev;
240 
241 		if (dev && state->status == VHOST_SCSI_DEV_REMOVING &&
242 		    !spdk_scsi_dev_has_pending_tasks(dev, NULL)) {
243 			/* detach the device from this session */
244 			spdk_scsi_dev_free_io_channels(dev);
245 			state->dev = NULL;
246 			state->status = VHOST_SCSI_DEV_REMOVED;
247 			/* try to detach it globally */
248 			spdk_vhost_lock();
249 			vhost_user_dev_foreach_session(&svsession->svdev->vdev,
250 						       vhost_scsi_session_process_removed,
251 						       vhost_scsi_dev_process_removed_cpl_cb,
252 						       (void *)(uintptr_t)i);
253 			spdk_vhost_unlock();
254 		}
255 	}
256 }
257 
258 static void
259 eventq_enqueue(struct spdk_vhost_scsi_session *svsession, unsigned scsi_dev_num,
260 	       uint32_t event, uint32_t reason)
261 {
262 	struct spdk_vhost_session *vsession = &svsession->vsession;
263 	struct spdk_vhost_virtqueue *vq;
264 	struct vring_desc *desc, *desc_table;
265 	struct virtio_scsi_event *desc_ev;
266 	uint32_t desc_table_size, req_size = 0;
267 	uint16_t req;
268 	int rc;
269 
270 	assert(scsi_dev_num < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS);
271 	vq = &vsession->virtqueue[VIRTIO_SCSI_EVENTQ];
272 
273 	if (vq->vring.desc == NULL || vhost_vq_avail_ring_get(vq, &req, 1) != 1) {
274 		SPDK_ERRLOG("%s: failed to send virtio event (no avail ring entries?).\n",
275 			    vsession->name);
276 		return;
277 	}
278 
279 	rc = vhost_vq_get_desc(vsession, vq, req, &desc, &desc_table, &desc_table_size);
280 	if (rc != 0 || desc->len < sizeof(*desc_ev)) {
281 		SPDK_ERRLOG("%s: invalid eventq descriptor at index %"PRIu16".\n",
282 			    vsession->name, req);
283 		goto out;
284 	}
285 
286 	desc_ev = vhost_gpa_to_vva(vsession, desc->addr, sizeof(*desc_ev));
287 	if (desc_ev == NULL) {
288 		SPDK_ERRLOG("%s: eventq descriptor at index %"PRIu16" points "
289 			    "to unmapped guest memory address %p.\n",
290 			    vsession->name, req, (void *)(uintptr_t)desc->addr);
291 		goto out;
292 	}
293 
294 	desc_ev->event = event;
295 	desc_ev->lun[0] = 1;
296 	desc_ev->lun[1] = scsi_dev_num;
297 	/* virtio LUN id 0 can refer either to the entire device
298 	 * or actual LUN 0 (the only supported by vhost for now)
299 	 */
300 	desc_ev->lun[2] = 0 >> 8;
301 	desc_ev->lun[3] = 0 & 0xFF;
302 	/* virtio doesn't specify any strict format for LUN id (bytes 2 and 3)
303 	 * current implementation relies on linux kernel sources
304 	 */
305 	memset(&desc_ev->lun[4], 0, 4);
306 	desc_ev->reason = reason;
307 	req_size = sizeof(*desc_ev);
308 
309 out:
310 	vhost_vq_used_ring_enqueue(vsession, vq, req, req_size);
311 }
312 
313 static void
314 submit_completion(struct spdk_vhost_scsi_task *task)
315 {
316 	struct spdk_vhost_session *vsession = &task->svsession->vsession;
317 
318 	vhost_vq_used_ring_enqueue(vsession, task->vq, task->req_idx,
319 				   task->used_len);
320 	SPDK_DEBUGLOG(vhost_scsi, "Finished task (%p) req_idx=%d\n", task, task->req_idx);
321 
322 	vhost_scsi_task_put(task);
323 }
324 
325 static void
326 vhost_scsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task)
327 {
328 	struct spdk_vhost_scsi_task *task = SPDK_CONTAINEROF(scsi_task, struct spdk_vhost_scsi_task, scsi);
329 
330 	submit_completion(task);
331 }
332 
333 static void
334 vhost_scsi_task_cpl(struct spdk_scsi_task *scsi_task)
335 {
336 	struct spdk_vhost_scsi_task *task = SPDK_CONTAINEROF(scsi_task, struct spdk_vhost_scsi_task, scsi);
337 
338 	/* The SCSI task has completed.  Do final processing and then post
339 	   notification to the virtqueue's "used" ring.
340 	 */
341 	task->resp->status = task->scsi.status;
342 
343 	if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
344 		memcpy(task->resp->sense, task->scsi.sense_data, task->scsi.sense_data_len);
345 		task->resp->sense_len = task->scsi.sense_data_len;
346 		SPDK_DEBUGLOG(vhost_scsi, "Task (%p) req_idx=%d failed - status=%u\n", task, task->req_idx,
347 			      task->scsi.status);
348 	}
349 	assert(task->scsi.transfer_len == task->scsi.length);
350 	task->resp->resid = task->scsi.length - task->scsi.data_transferred;
351 
352 	submit_completion(task);
353 }
354 
355 static void
356 task_submit(struct spdk_vhost_scsi_task *task)
357 {
358 	task->resp->response = VIRTIO_SCSI_S_OK;
359 	spdk_scsi_dev_queue_task(task->scsi_dev, &task->scsi);
360 }
361 
362 static void
363 mgmt_task_submit(struct spdk_vhost_scsi_task *task, enum spdk_scsi_task_func func)
364 {
365 	task->tmf_resp->response = VIRTIO_SCSI_S_OK;
366 	task->scsi.function = func;
367 	spdk_scsi_dev_queue_mgmt_task(task->scsi_dev, &task->scsi);
368 }
369 
370 static void
371 invalid_request(struct spdk_vhost_scsi_task *task)
372 {
373 	struct spdk_vhost_session *vsession = &task->svsession->vsession;
374 
375 	vhost_vq_used_ring_enqueue(vsession, task->vq, task->req_idx,
376 				   task->used_len);
377 	vhost_scsi_task_put(task);
378 
379 	SPDK_DEBUGLOG(vhost_scsi, "Invalid request (status=%" PRIu8")\n",
380 		      task->resp ? task->resp->response : -1);
381 }
382 
383 static int
384 vhost_scsi_task_init_target(struct spdk_vhost_scsi_task *task, const __u8 *lun)
385 {
386 	struct spdk_vhost_scsi_session *svsession = task->svsession;
387 	struct spdk_scsi_dev_session_state *state;
388 	uint16_t lun_id = (((uint16_t)lun[2] << 8) | lun[3]) & 0x3FFF;
389 
390 	SPDK_LOGDUMP(vhost_scsi_queue, "LUN", lun, 8);
391 
392 	/* First byte must be 1 and second is target */
393 	if (lun[0] != 1 || lun[1] >= SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
394 		return -1;
395 	}
396 
397 	state = &svsession->scsi_dev_state[lun[1]];
398 	task->scsi_dev = state->dev;
399 	if (state->dev == NULL || state->status != VHOST_SCSI_DEV_PRESENT) {
400 		/* If dev has been hotdetached, return 0 to allow sending
401 		 * additional hotremove event via sense codes.
402 		 */
403 		return state->status != VHOST_SCSI_DEV_EMPTY ? 0 : -1;
404 	}
405 
406 	task->scsi.target_port = spdk_scsi_dev_find_port_by_id(task->scsi_dev, 0);
407 	task->scsi.lun = spdk_scsi_dev_get_lun(state->dev, lun_id);
408 	return 0;
409 }
410 
411 static void
412 process_ctrl_request(struct spdk_vhost_scsi_task *task)
413 {
414 	struct spdk_vhost_session *vsession = &task->svsession->vsession;
415 	struct vring_desc *desc, *desc_table;
416 	struct virtio_scsi_ctrl_tmf_req *ctrl_req;
417 	struct virtio_scsi_ctrl_an_resp *an_resp;
418 	uint32_t desc_table_size, used_len = 0;
419 	int rc;
420 
421 	spdk_scsi_task_construct(&task->scsi, vhost_scsi_task_mgmt_cpl, vhost_scsi_task_free_cb);
422 	rc = vhost_vq_get_desc(vsession, task->vq, task->req_idx, &desc, &desc_table,
423 			       &desc_table_size);
424 	if (spdk_unlikely(rc != 0)) {
425 		SPDK_ERRLOG("%s: invalid controlq descriptor at index %d.\n",
426 			    vsession->name, task->req_idx);
427 		goto out;
428 	}
429 
430 	ctrl_req = vhost_gpa_to_vva(vsession, desc->addr, sizeof(*ctrl_req));
431 	if (ctrl_req == NULL) {
432 		SPDK_ERRLOG("%s: invalid task management request at index %d.\n",
433 			    vsession->name, task->req_idx);
434 		goto out;
435 	}
436 
437 	SPDK_DEBUGLOG(vhost_scsi_queue,
438 		      "Processing controlq descriptor: desc %d/%p, desc_addr %p, len %d, flags %d, last_used_idx %d; kickfd %d; size %d\n",
439 		      task->req_idx, desc, (void *)desc->addr, desc->len, desc->flags, task->vq->last_used_idx,
440 		      task->vq->vring.kickfd, task->vq->vring.size);
441 	SPDK_LOGDUMP(vhost_scsi_queue, "Request descriptor", (uint8_t *)ctrl_req, desc->len);
442 
443 	vhost_scsi_task_init_target(task, ctrl_req->lun);
444 
445 	vhost_vring_desc_get_next(&desc, desc_table, desc_table_size);
446 	if (spdk_unlikely(desc == NULL)) {
447 		SPDK_ERRLOG("%s: no response descriptor for controlq request %d.\n",
448 			    vsession->name, task->req_idx);
449 		goto out;
450 	}
451 
452 	/* Process the TMF request */
453 	switch (ctrl_req->type) {
454 	case VIRTIO_SCSI_T_TMF:
455 		task->tmf_resp = vhost_gpa_to_vva(vsession, desc->addr, sizeof(*task->tmf_resp));
456 		if (spdk_unlikely(desc->len < sizeof(struct virtio_scsi_ctrl_tmf_resp) || task->tmf_resp == NULL)) {
457 			SPDK_ERRLOG("%s: TMF response descriptor at index %d points to invalid guest memory region\n",
458 				    vsession->name, task->req_idx);
459 			goto out;
460 		}
461 
462 		/* Check if we are processing a valid request */
463 		if (task->scsi_dev == NULL) {
464 			task->tmf_resp->response = VIRTIO_SCSI_S_BAD_TARGET;
465 			break;
466 		}
467 
468 		switch (ctrl_req->subtype) {
469 		case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
470 			/* Handle LUN reset */
471 			SPDK_DEBUGLOG(vhost_scsi_queue, "%s: LUN reset\n", vsession->name);
472 
473 			mgmt_task_submit(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
474 			return;
475 		default:
476 			task->tmf_resp->response = VIRTIO_SCSI_S_ABORTED;
477 			/* Unsupported command */
478 			SPDK_DEBUGLOG(vhost_scsi_queue, "%s: unsupported TMF command %x\n",
479 				      vsession->name, ctrl_req->subtype);
480 			break;
481 		}
482 		break;
483 	case VIRTIO_SCSI_T_AN_QUERY:
484 	case VIRTIO_SCSI_T_AN_SUBSCRIBE: {
485 		an_resp = vhost_gpa_to_vva(vsession, desc->addr, sizeof(*an_resp));
486 		if (spdk_unlikely(desc->len < sizeof(struct virtio_scsi_ctrl_an_resp) || an_resp == NULL)) {
487 			SPDK_WARNLOG("%s: asynchronous response descriptor points to invalid guest memory region\n",
488 				     vsession->name);
489 			goto out;
490 		}
491 
492 		an_resp->response = VIRTIO_SCSI_S_ABORTED;
493 		break;
494 	}
495 	default:
496 		SPDK_DEBUGLOG(vhost_scsi_queue, "%s: Unsupported control command %x\n",
497 			      vsession->name, ctrl_req->type);
498 		break;
499 	}
500 
501 	used_len = sizeof(struct virtio_scsi_ctrl_tmf_resp);
502 out:
503 	vhost_vq_used_ring_enqueue(vsession, task->vq, task->req_idx, used_len);
504 	vhost_scsi_task_put(task);
505 }
506 
507 /*
508  * Process task's descriptor chain and setup data related fields.
509  * Return
510  *   -1 if request is invalid and must be aborted,
511  *    0 if all data are set.
512  */
513 static int
514 task_data_setup(struct spdk_vhost_scsi_task *task,
515 		struct virtio_scsi_cmd_req **req)
516 {
517 	struct spdk_vhost_session *vsession = &task->svsession->vsession;
518 	struct vring_desc *desc, *desc_table;
519 	struct iovec *iovs = task->iovs;
520 	uint16_t iovcnt = 0;
521 	uint32_t desc_table_len, len = 0;
522 	int rc;
523 
524 	spdk_scsi_task_construct(&task->scsi, vhost_scsi_task_cpl, vhost_scsi_task_free_cb);
525 
526 	rc = vhost_vq_get_desc(vsession, task->vq, task->req_idx, &desc, &desc_table, &desc_table_len);
527 	/* First descriptor must be readable */
528 	if (spdk_unlikely(rc != 0  || vhost_vring_desc_is_wr(desc) ||
529 			  desc->len < sizeof(struct virtio_scsi_cmd_req))) {
530 		SPDK_WARNLOG("%s: invalid first request descriptor at index %"PRIu16".\n",
531 			     vsession->name, task->req_idx);
532 		goto invalid_task;
533 	}
534 
535 	*req = vhost_gpa_to_vva(vsession, desc->addr, sizeof(**req));
536 	if (spdk_unlikely(*req == NULL)) {
537 		SPDK_WARNLOG("%s: request descriptor at index %d points to invalid guest memory region\n",
538 			     vsession->name, task->req_idx);
539 		goto invalid_task;
540 	}
541 
542 	/* Each request must have at least 2 descriptors (e.g. request and response) */
543 	vhost_vring_desc_get_next(&desc, desc_table, desc_table_len);
544 	if (desc == NULL) {
545 		SPDK_WARNLOG("%s: descriptor chain at index %d contains neither payload nor response buffer.\n",
546 			     vsession->name, task->req_idx);
547 		goto invalid_task;
548 	}
549 	task->scsi.dxfer_dir = vhost_vring_desc_is_wr(desc) ? SPDK_SCSI_DIR_FROM_DEV :
550 			       SPDK_SCSI_DIR_TO_DEV;
551 	task->scsi.iovs = iovs;
552 
553 	if (task->scsi.dxfer_dir == SPDK_SCSI_DIR_FROM_DEV) {
554 		/*
555 		 * FROM_DEV (READ): [RD_req][WR_resp][WR_buf0]...[WR_bufN]
556 		 */
557 		task->resp = vhost_gpa_to_vva(vsession, desc->addr, sizeof(*task->resp));
558 		if (spdk_unlikely(desc->len < sizeof(struct virtio_scsi_cmd_resp) || task->resp == NULL)) {
559 			SPDK_WARNLOG("%s: response descriptor at index %d points to invalid guest memory region\n",
560 				     vsession->name, task->req_idx);
561 			goto invalid_task;
562 		}
563 		rc = vhost_vring_desc_get_next(&desc, desc_table, desc_table_len);
564 		if (spdk_unlikely(rc != 0)) {
565 			SPDK_WARNLOG("%s: invalid descriptor chain at request index %d (descriptor id overflow?).\n",
566 				     vsession->name, task->req_idx);
567 			goto invalid_task;
568 		}
569 
570 		if (desc == NULL) {
571 			/*
572 			 * TEST UNIT READY command and some others might not contain any payload and this is not an error.
573 			 */
574 			SPDK_DEBUGLOG(vhost_scsi_data,
575 				      "No payload descriptors for FROM DEV command req_idx=%"PRIu16".\n", task->req_idx);
576 			SPDK_LOGDUMP(vhost_scsi_data, "CDB=", (*req)->cdb, VIRTIO_SCSI_CDB_SIZE);
577 			task->used_len = sizeof(struct virtio_scsi_cmd_resp);
578 			task->scsi.iovcnt = 1;
579 			task->scsi.iovs[0].iov_len = 0;
580 			task->scsi.length = 0;
581 			task->scsi.transfer_len = 0;
582 			return 0;
583 		}
584 
585 		/* All remaining descriptors are data. */
586 		while (desc) {
587 			if (spdk_unlikely(!vhost_vring_desc_is_wr(desc))) {
588 				SPDK_WARNLOG("%s: FROM DEV cmd: descriptor nr %" PRIu16" in payload chain is read only.\n",
589 					     vsession->name, iovcnt);
590 				goto invalid_task;
591 			}
592 
593 			if (spdk_unlikely(vhost_vring_desc_to_iov(vsession, iovs, &iovcnt, desc))) {
594 				goto invalid_task;
595 			}
596 			len += desc->len;
597 
598 			rc = vhost_vring_desc_get_next(&desc, desc_table, desc_table_len);
599 			if (spdk_unlikely(rc != 0)) {
600 				SPDK_WARNLOG("%s: invalid payload in descriptor chain starting at index %d.\n",
601 					     vsession->name, task->req_idx);
602 				goto invalid_task;
603 			}
604 		}
605 
606 		task->used_len = sizeof(struct virtio_scsi_cmd_resp) + len;
607 	} else {
608 		SPDK_DEBUGLOG(vhost_scsi_data, "TO DEV");
609 		/*
610 		 * TO_DEV (WRITE):[RD_req][RD_buf0]...[RD_bufN][WR_resp]
611 		 * No need to check descriptor WR flag as this is done while setting scsi.dxfer_dir.
612 		 */
613 
614 		/* Process descriptors up to response. */
615 		while (!vhost_vring_desc_is_wr(desc)) {
616 			if (spdk_unlikely(vhost_vring_desc_to_iov(vsession, iovs, &iovcnt, desc))) {
617 				goto invalid_task;
618 			}
619 			len += desc->len;
620 
621 			vhost_vring_desc_get_next(&desc, desc_table, desc_table_len);
622 			if (spdk_unlikely(desc == NULL)) {
623 				SPDK_WARNLOG("%s: TO_DEV cmd: no response descriptor.\n", vsession->name);
624 				goto invalid_task;
625 			}
626 		}
627 
628 		task->resp = vhost_gpa_to_vva(vsession, desc->addr, sizeof(*task->resp));
629 		if (spdk_unlikely(desc->len < sizeof(struct virtio_scsi_cmd_resp) || task->resp == NULL)) {
630 			SPDK_WARNLOG("%s: response descriptor at index %d points to invalid guest memory region\n",
631 				     vsession->name, task->req_idx);
632 			goto invalid_task;
633 		}
634 
635 		task->used_len = sizeof(struct virtio_scsi_cmd_resp);
636 	}
637 
638 	task->scsi.iovcnt = iovcnt;
639 	task->scsi.length = len;
640 	task->scsi.transfer_len = len;
641 	return 0;
642 
643 invalid_task:
644 	SPDK_DEBUGLOG(vhost_scsi_data, "%s: Invalid task at index %"PRIu16".\n",
645 		      vsession->name, task->req_idx);
646 	return -1;
647 }
648 
649 static int
650 process_request(struct spdk_vhost_scsi_task *task)
651 {
652 	struct virtio_scsi_cmd_req *req;
653 	int result;
654 
655 	result = task_data_setup(task, &req);
656 	if (result) {
657 		return result;
658 	}
659 
660 	result = vhost_scsi_task_init_target(task, req->lun);
661 	if (spdk_unlikely(result != 0)) {
662 		task->resp->response = VIRTIO_SCSI_S_BAD_TARGET;
663 		return -1;
664 	}
665 
666 	task->scsi.cdb = req->cdb;
667 	SPDK_LOGDUMP(vhost_scsi_data, "request CDB", req->cdb, VIRTIO_SCSI_CDB_SIZE);
668 
669 	if (spdk_unlikely(task->scsi.lun == NULL)) {
670 		spdk_scsi_task_process_null_lun(&task->scsi);
671 		task->resp->response = VIRTIO_SCSI_S_OK;
672 		return 1;
673 	}
674 
675 	return 0;
676 }
677 
678 static void
679 process_scsi_task(struct spdk_vhost_session *vsession,
680 		  struct spdk_vhost_virtqueue *vq,
681 		  uint16_t req_idx)
682 {
683 	struct spdk_vhost_scsi_task *task;
684 	int result;
685 
686 	task = &((struct spdk_vhost_scsi_task *)vq->tasks)[req_idx];
687 	if (spdk_unlikely(task->used)) {
688 		SPDK_ERRLOG("%s: request with idx '%"PRIu16"' is already pending.\n",
689 			    vsession->name, req_idx);
690 		vhost_vq_used_ring_enqueue(vsession, vq, req_idx, 0);
691 		return;
692 	}
693 
694 	vsession->task_cnt++;
695 	scsi_task_init(task);
696 
697 	if (spdk_unlikely(vq->vring_idx == VIRTIO_SCSI_CONTROLQ)) {
698 		process_ctrl_request(task);
699 	} else {
700 		result = process_request(task);
701 		if (likely(result == 0)) {
702 			task_submit(task);
703 			SPDK_DEBUGLOG(vhost_scsi, "====== Task %p req_idx %d submitted ======\n", task,
704 				      task->req_idx);
705 		} else if (result > 0) {
706 			vhost_scsi_task_cpl(&task->scsi);
707 			SPDK_DEBUGLOG(vhost_scsi, "====== Task %p req_idx %d finished early ======\n", task,
708 				      task->req_idx);
709 		} else {
710 			invalid_request(task);
711 			SPDK_DEBUGLOG(vhost_scsi, "====== Task %p req_idx %d failed ======\n", task,
712 				      task->req_idx);
713 		}
714 	}
715 }
716 
717 static int
718 submit_inflight_desc(struct spdk_vhost_scsi_session *svsession,
719 		     struct spdk_vhost_virtqueue *vq)
720 {
721 	struct spdk_vhost_session *vsession;
722 	spdk_vhost_resubmit_info *resubmit;
723 	spdk_vhost_resubmit_desc *resubmit_list;
724 	uint16_t req_idx;
725 	int i, resubmit_cnt;
726 
727 	resubmit = vq->vring_inflight.resubmit_inflight;
728 	if (spdk_likely(resubmit == NULL || resubmit->resubmit_list == NULL ||
729 			resubmit->resubmit_num == 0)) {
730 		return 0;
731 	}
732 
733 	resubmit_list = resubmit->resubmit_list;
734 	vsession = &svsession->vsession;
735 
736 	for (i = resubmit->resubmit_num - 1; i >= 0; --i) {
737 		req_idx = resubmit_list[resubmit->resubmit_num].index;
738 		SPDK_DEBUGLOG(vhost_scsi, "====== Start processing resubmit request idx %"PRIu16"======\n",
739 			      req_idx);
740 
741 		if (spdk_unlikely(req_idx >= vq->vring.size)) {
742 			SPDK_ERRLOG("%s: request idx '%"PRIu16"' exceeds virtqueue size (%"PRIu16").\n",
743 				    vsession->name, req_idx, vq->vring.size);
744 			vhost_vq_used_ring_enqueue(vsession, vq, req_idx, 0);
745 			continue;
746 		}
747 
748 		process_scsi_task(vsession, vq, req_idx);
749 	}
750 	resubmit_cnt = resubmit->resubmit_num;
751 	resubmit->resubmit_num = 0;
752 	return resubmit_cnt;
753 }
754 
755 static int
756 process_vq(struct spdk_vhost_scsi_session *svsession, struct spdk_vhost_virtqueue *vq)
757 {
758 	struct spdk_vhost_session *vsession = &svsession->vsession;
759 	uint16_t reqs[32];
760 	uint16_t reqs_cnt, i;
761 	int resubmit_cnt;
762 
763 	resubmit_cnt = submit_inflight_desc(svsession, vq);
764 
765 	reqs_cnt = vhost_vq_avail_ring_get(vq, reqs, SPDK_COUNTOF(reqs));
766 	assert(reqs_cnt <= 32);
767 
768 	for (i = 0; i < reqs_cnt; i++) {
769 		SPDK_DEBUGLOG(vhost_scsi, "====== Starting processing request idx %"PRIu16"======\n",
770 			      reqs[i]);
771 
772 		if (spdk_unlikely(reqs[i] >= vq->vring.size)) {
773 			SPDK_ERRLOG("%s: request idx '%"PRIu16"' exceeds virtqueue size (%"PRIu16").\n",
774 				    vsession->name, reqs[i], vq->vring.size);
775 			vhost_vq_used_ring_enqueue(vsession, vq, reqs[i], 0);
776 			continue;
777 		}
778 
779 		rte_vhost_set_inflight_desc_split(vsession->vid, vq->vring_idx, reqs[i]);
780 
781 		process_scsi_task(vsession, vq, reqs[i]);
782 	}
783 
784 	return reqs_cnt > 0 ? reqs_cnt : resubmit_cnt;
785 }
786 
787 static int
788 vdev_mgmt_worker(void *arg)
789 {
790 	struct spdk_vhost_scsi_session *svsession = arg;
791 	struct spdk_vhost_session *vsession = &svsession->vsession;
792 	int rc = 0;
793 
794 	process_removed_devs(svsession);
795 
796 	if (vsession->virtqueue[VIRTIO_SCSI_EVENTQ].vring.desc) {
797 		vhost_vq_used_signal(vsession, &vsession->virtqueue[VIRTIO_SCSI_EVENTQ]);
798 	}
799 
800 	if (vsession->virtqueue[VIRTIO_SCSI_CONTROLQ].vring.desc) {
801 		rc = process_vq(svsession, &vsession->virtqueue[VIRTIO_SCSI_CONTROLQ]);
802 		vhost_vq_used_signal(vsession, &vsession->virtqueue[VIRTIO_SCSI_CONTROLQ]);
803 	}
804 
805 	return rc > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
806 }
807 
808 static int
809 vdev_worker(void *arg)
810 {
811 	struct spdk_vhost_scsi_session *svsession = arg;
812 	struct spdk_vhost_session *vsession = &svsession->vsession;
813 	uint32_t q_idx;
814 	int rc = 0;
815 
816 	for (q_idx = VIRTIO_SCSI_REQUESTQ; q_idx < vsession->max_queues; q_idx++) {
817 		rc = process_vq(svsession, &vsession->virtqueue[q_idx]);
818 		vhost_session_vq_used_signal(&vsession->virtqueue[q_idx]);
819 	}
820 
821 	return rc > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
822 }
823 
824 static struct spdk_vhost_scsi_dev *
825 to_scsi_dev(struct spdk_vhost_dev *ctrlr)
826 {
827 	if (ctrlr == NULL) {
828 		return NULL;
829 	}
830 
831 	if (ctrlr->backend->type != VHOST_BACKEND_SCSI) {
832 		SPDK_ERRLOG("%s: not a vhost-scsi device.\n", ctrlr->name);
833 		return NULL;
834 	}
835 
836 	return SPDK_CONTAINEROF(ctrlr, struct spdk_vhost_scsi_dev, vdev);
837 }
838 
839 static struct spdk_vhost_scsi_session *
840 to_scsi_session(struct spdk_vhost_session *vsession)
841 {
842 	assert(vsession->vdev->backend->type == VHOST_BACKEND_SCSI);
843 	return (struct spdk_vhost_scsi_session *)vsession;
844 }
845 
846 int
847 spdk_vhost_scsi_dev_construct(const char *name, const char *cpumask)
848 {
849 	struct spdk_vhost_scsi_dev *svdev = calloc(1, sizeof(*svdev));
850 	int rc;
851 
852 	if (svdev == NULL) {
853 		return -ENOMEM;
854 	}
855 
856 	svdev->vdev.virtio_features = SPDK_VHOST_SCSI_FEATURES;
857 	svdev->vdev.disabled_features = SPDK_VHOST_SCSI_DISABLED_FEATURES;
858 	svdev->vdev.protocol_features = SPDK_VHOST_SCSI_PROTOCOL_FEATURES;
859 
860 	spdk_vhost_lock();
861 	rc = vhost_dev_register(&svdev->vdev, name, cpumask, NULL,
862 				&spdk_vhost_scsi_device_backend,
863 				&spdk_vhost_scsi_user_device_backend);
864 	if (rc) {
865 		free(svdev);
866 		spdk_vhost_unlock();
867 		return rc;
868 	}
869 
870 	svdev->registered = true;
871 
872 	spdk_vhost_unlock();
873 	return rc;
874 }
875 
876 static int
877 vhost_scsi_dev_remove(struct spdk_vhost_dev *vdev)
878 {
879 	struct spdk_vhost_scsi_dev *svdev = to_scsi_dev(vdev);
880 	struct spdk_vhost_user_dev *user_dev = vdev->ctxt;
881 	int rc, i;
882 
883 	if (user_dev->pending_async_op_num) {
884 		return -EBUSY;
885 	}
886 
887 	assert(svdev != NULL);
888 	for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; ++i) {
889 		if (svdev->scsi_dev_state[i].dev) {
890 			if (vdev->registered) {
891 				SPDK_ERRLOG("%s: SCSI target %d is still present.\n", vdev->name, i);
892 				return -EBUSY;
893 			}
894 
895 			rc = spdk_vhost_scsi_dev_remove_tgt(vdev, i, NULL, NULL);
896 			if (rc != 0) {
897 				SPDK_ERRLOG("%s: failed to force-remove target %d\n", vdev->name, i);
898 				return rc;
899 			}
900 		}
901 	}
902 
903 	rc = vhost_dev_unregister(vdev);
904 	if (rc != 0) {
905 		return rc;
906 	}
907 	svdev->registered = false;
908 
909 	if (svdev->ref == 0) {
910 		free(svdev);
911 	}
912 
913 	return 0;
914 }
915 
916 struct spdk_scsi_dev *
917 spdk_vhost_scsi_dev_get_tgt(struct spdk_vhost_dev *vdev, uint8_t num)
918 {
919 	struct spdk_vhost_scsi_dev *svdev;
920 
921 	assert(num < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS);
922 	svdev = to_scsi_dev(vdev);
923 	assert(svdev != NULL);
924 	if (svdev->scsi_dev_state[num].status != VHOST_SCSI_DEV_PRESENT) {
925 		return NULL;
926 	}
927 
928 	assert(svdev->scsi_dev_state[num].dev != NULL);
929 	return svdev->scsi_dev_state[num].dev;
930 }
931 
932 static unsigned
933 get_scsi_dev_num(const struct spdk_vhost_scsi_dev *svdev,
934 		 const struct spdk_scsi_lun *lun)
935 {
936 	const struct spdk_scsi_dev *scsi_dev;
937 	unsigned scsi_dev_num;
938 
939 	assert(lun != NULL);
940 	assert(svdev != NULL);
941 	scsi_dev = spdk_scsi_lun_get_dev(lun);
942 	for (scsi_dev_num = 0; scsi_dev_num < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; scsi_dev_num++) {
943 		if (svdev->scsi_dev_state[scsi_dev_num].dev == scsi_dev) {
944 			break;
945 		}
946 	}
947 
948 	return scsi_dev_num;
949 }
950 
951 static void
952 vhost_scsi_lun_resize(const struct spdk_scsi_lun *lun, void *arg)
953 {
954 	struct spdk_vhost_scsi_dev *svdev = arg;
955 	unsigned scsi_dev_num;
956 
957 	scsi_dev_num = get_scsi_dev_num(svdev, lun);
958 	if (scsi_dev_num == SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
959 		/* The entire device has been already removed. */
960 		return;
961 	}
962 
963 	vhost_scsi_dev_param_changed(&svdev->vdev, scsi_dev_num);
964 }
965 
966 static void
967 vhost_scsi_lun_hotremove(const struct spdk_scsi_lun *lun, void *arg)
968 {
969 	struct spdk_vhost_scsi_dev *svdev = arg;
970 	unsigned scsi_dev_num;
971 
972 	scsi_dev_num = get_scsi_dev_num(svdev, lun);
973 	if (scsi_dev_num == SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
974 		/* The entire device has been already removed. */
975 		return;
976 	}
977 
978 	/* remove entire device */
979 	spdk_vhost_scsi_dev_remove_tgt(&svdev->vdev, scsi_dev_num, NULL, NULL);
980 }
981 
982 static void
983 vhost_scsi_dev_add_tgt_cpl_cb(struct spdk_vhost_dev *vdev, void *ctx)
984 {
985 	unsigned scsi_tgt_num = (unsigned)(uintptr_t)ctx;
986 	struct spdk_vhost_scsi_dev *svdev = SPDK_CONTAINEROF(vdev,
987 					    struct spdk_vhost_scsi_dev, vdev);
988 	struct spdk_scsi_dev_vhost_state *vhost_sdev;
989 
990 	vhost_sdev = &svdev->scsi_dev_state[scsi_tgt_num];
991 
992 	/* All sessions have added the target */
993 	assert(vhost_sdev->status == VHOST_SCSI_DEV_ADDING);
994 	vhost_sdev->status = VHOST_SCSI_DEV_PRESENT;
995 	svdev->ref++;
996 }
997 
998 static int
999 vhost_scsi_session_add_tgt(struct spdk_vhost_dev *vdev,
1000 			   struct spdk_vhost_session *vsession, void *ctx)
1001 {
1002 	unsigned scsi_tgt_num = (unsigned)(uintptr_t)ctx;
1003 	struct spdk_vhost_scsi_session *svsession = (struct spdk_vhost_scsi_session *)vsession;
1004 	struct spdk_scsi_dev_session_state *session_sdev = &svsession->scsi_dev_state[scsi_tgt_num];
1005 	struct spdk_scsi_dev_vhost_state *vhost_sdev;
1006 	int rc;
1007 
1008 	if (!vsession->started || session_sdev->dev != NULL) {
1009 		/* Nothing to do. */
1010 		return 0;
1011 	}
1012 
1013 	vhost_sdev = &svsession->svdev->scsi_dev_state[scsi_tgt_num];
1014 	session_sdev->dev = vhost_sdev->dev;
1015 	session_sdev->status = VHOST_SCSI_DEV_PRESENT;
1016 
1017 	rc = spdk_scsi_dev_allocate_io_channels(svsession->scsi_dev_state[scsi_tgt_num].dev);
1018 	if (rc != 0) {
1019 		SPDK_ERRLOG("%s: Couldn't allocate io channel for SCSI target %u.\n",
1020 			    vsession->name, scsi_tgt_num);
1021 
1022 		/* unset the SCSI target so that all I/O to it will be rejected */
1023 		session_sdev->dev = NULL;
1024 		/* Set status to EMPTY so that we won't reply with SCSI hotremove
1025 		 * sense codes - the device hasn't ever been added.
1026 		 */
1027 		session_sdev->status = VHOST_SCSI_DEV_EMPTY;
1028 
1029 		/* Return with no error. We'll continue allocating io_channels for
1030 		 * other sessions on this device in hopes they succeed. The sessions
1031 		 * that failed to allocate io_channels simply won't be able to
1032 		 * detect the SCSI target, nor do any I/O to it.
1033 		 */
1034 		return 0;
1035 	}
1036 
1037 	if (vhost_dev_has_feature(vsession, VIRTIO_SCSI_F_HOTPLUG)) {
1038 		eventq_enqueue(svsession, scsi_tgt_num,
1039 			       VIRTIO_SCSI_T_TRANSPORT_RESET, VIRTIO_SCSI_EVT_RESET_RESCAN);
1040 	} else {
1041 		SPDK_NOTICELOG("%s: driver does not support hotplug. "
1042 			       "Please restart it or perform a rescan.\n",
1043 			       vsession->name);
1044 	}
1045 
1046 	return 0;
1047 }
1048 
1049 int
1050 spdk_vhost_scsi_dev_add_tgt(struct spdk_vhost_dev *vdev, int scsi_tgt_num,
1051 			    const char *bdev_name)
1052 {
1053 	struct spdk_vhost_scsi_dev *svdev;
1054 	struct spdk_scsi_dev_vhost_state *state;
1055 	char target_name[SPDK_SCSI_DEV_MAX_NAME];
1056 	int lun_id_list[1];
1057 	const char *bdev_names_list[1];
1058 
1059 	svdev = to_scsi_dev(vdev);
1060 	if (!svdev) {
1061 		SPDK_ERRLOG("Before adding a SCSI target, there should be a SCSI device.");
1062 		return -EINVAL;
1063 	}
1064 
1065 	if (scsi_tgt_num < 0) {
1066 		for (scsi_tgt_num = 0; scsi_tgt_num < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; scsi_tgt_num++) {
1067 			if (svdev->scsi_dev_state[scsi_tgt_num].dev == NULL) {
1068 				break;
1069 			}
1070 		}
1071 
1072 		if (scsi_tgt_num == SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
1073 			SPDK_ERRLOG("%s: all SCSI target slots are already in use.\n", vdev->name);
1074 			return -ENOSPC;
1075 		}
1076 	} else {
1077 		if (scsi_tgt_num >= SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
1078 			SPDK_ERRLOG("%s: SCSI target number is too big (got %d, max %d), started from 0.\n",
1079 				    vdev->name, scsi_tgt_num, SPDK_VHOST_SCSI_CTRLR_MAX_DEVS - 1);
1080 			return -EINVAL;
1081 		}
1082 	}
1083 
1084 	if (bdev_name == NULL) {
1085 		SPDK_ERRLOG("No lun name specified\n");
1086 		return -EINVAL;
1087 	}
1088 
1089 	state = &svdev->scsi_dev_state[scsi_tgt_num];
1090 	if (state->dev != NULL) {
1091 		SPDK_ERRLOG("%s: SCSI target %u already occupied\n", vdev->name, scsi_tgt_num);
1092 		return -EEXIST;
1093 	}
1094 
1095 	/*
1096 	 * At this stage only one LUN per target
1097 	 */
1098 	snprintf(target_name, sizeof(target_name), "Target %u", scsi_tgt_num);
1099 	lun_id_list[0] = 0;
1100 	bdev_names_list[0] = (char *)bdev_name;
1101 
1102 	state->status = VHOST_SCSI_DEV_ADDING;
1103 	state->dev = spdk_scsi_dev_construct_ext(target_name, bdev_names_list, lun_id_list, 1,
1104 			SPDK_SPC_PROTOCOL_IDENTIFIER_SAS,
1105 			vhost_scsi_lun_resize, svdev,
1106 			vhost_scsi_lun_hotremove, svdev);
1107 
1108 	if (state->dev == NULL) {
1109 		state->status = VHOST_SCSI_DEV_EMPTY;
1110 		SPDK_ERRLOG("%s: couldn't create SCSI target %u using bdev '%s'\n",
1111 			    vdev->name, scsi_tgt_num, bdev_name);
1112 		return -EINVAL;
1113 	}
1114 	spdk_scsi_dev_add_port(state->dev, 0, "vhost");
1115 
1116 	SPDK_INFOLOG(vhost, "%s: added SCSI target %u using bdev '%s'\n",
1117 		     vdev->name, scsi_tgt_num, bdev_name);
1118 
1119 	vhost_user_dev_foreach_session(vdev, vhost_scsi_session_add_tgt,
1120 				       vhost_scsi_dev_add_tgt_cpl_cb,
1121 				       (void *)(uintptr_t)scsi_tgt_num);
1122 	return scsi_tgt_num;
1123 }
1124 
1125 struct scsi_tgt_hotplug_ctx {
1126 	unsigned scsi_tgt_num;
1127 	bool async_fini;
1128 };
1129 
1130 static void
1131 vhost_scsi_dev_remove_tgt_cpl_cb(struct spdk_vhost_dev *vdev, void *_ctx)
1132 {
1133 	struct scsi_tgt_hotplug_ctx *ctx = _ctx;
1134 	struct spdk_vhost_scsi_dev *svdev = SPDK_CONTAINEROF(vdev,
1135 					    struct spdk_vhost_scsi_dev, vdev);
1136 
1137 	if (!ctx->async_fini) {
1138 		/* there aren't any active sessions, so remove the dev and exit */
1139 		remove_scsi_tgt(svdev, ctx->scsi_tgt_num);
1140 	}
1141 
1142 	free(ctx);
1143 }
1144 
1145 static int
1146 vhost_scsi_session_remove_tgt(struct spdk_vhost_dev *vdev,
1147 			      struct spdk_vhost_session *vsession, void *_ctx)
1148 {
1149 	struct scsi_tgt_hotplug_ctx *ctx = _ctx;
1150 	unsigned scsi_tgt_num = ctx->scsi_tgt_num;
1151 	struct spdk_vhost_scsi_session *svsession = (struct spdk_vhost_scsi_session *)vsession;
1152 	struct spdk_scsi_dev_session_state *state = &svsession->scsi_dev_state[scsi_tgt_num];
1153 
1154 	if (!vsession->started || state->dev == NULL) {
1155 		/* Nothing to do */
1156 		return 0;
1157 	}
1158 
1159 	/* Mark the target for removal */
1160 	assert(state->status == VHOST_SCSI_DEV_PRESENT);
1161 	state->status = VHOST_SCSI_DEV_REMOVING;
1162 
1163 	/* Send a hotremove virtio event */
1164 	if (vhost_dev_has_feature(vsession, VIRTIO_SCSI_F_HOTPLUG)) {
1165 		eventq_enqueue(svsession, scsi_tgt_num,
1166 			       VIRTIO_SCSI_T_TRANSPORT_RESET, VIRTIO_SCSI_EVT_RESET_REMOVED);
1167 	}
1168 
1169 	/* Wait for the session's management poller to remove the target after
1170 	 * all its pending I/O has finished.
1171 	 */
1172 	ctx->async_fini = true;
1173 	return 0;
1174 }
1175 
1176 int
1177 spdk_vhost_scsi_dev_remove_tgt(struct spdk_vhost_dev *vdev, unsigned scsi_tgt_num,
1178 			       spdk_vhost_event_fn cb_fn, void *cb_arg)
1179 {
1180 	struct spdk_vhost_scsi_dev *svdev;
1181 	struct spdk_scsi_dev_vhost_state *scsi_dev_state;
1182 	struct scsi_tgt_hotplug_ctx *ctx;
1183 
1184 	if (scsi_tgt_num >= SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
1185 		SPDK_ERRLOG("%s: invalid SCSI target number %d\n", vdev->name, scsi_tgt_num);
1186 		return -EINVAL;
1187 	}
1188 
1189 	svdev = to_scsi_dev(vdev);
1190 	if (!svdev) {
1191 		SPDK_ERRLOG("An invalid SCSI device that removing from a SCSI target.");
1192 		return -EINVAL;
1193 	}
1194 
1195 	scsi_dev_state = &svdev->scsi_dev_state[scsi_tgt_num];
1196 
1197 	if (scsi_dev_state->status != VHOST_SCSI_DEV_PRESENT) {
1198 		return -EBUSY;
1199 	}
1200 
1201 	if (scsi_dev_state->dev == NULL || scsi_dev_state->status == VHOST_SCSI_DEV_ADDING) {
1202 		SPDK_ERRLOG("%s: SCSI target %u is not occupied\n", vdev->name, scsi_tgt_num);
1203 		return -ENODEV;
1204 	}
1205 
1206 	assert(scsi_dev_state->status != VHOST_SCSI_DEV_EMPTY);
1207 	ctx = calloc(1, sizeof(*ctx));
1208 	if (ctx == NULL) {
1209 		SPDK_ERRLOG("calloc failed\n");
1210 		return -ENOMEM;
1211 	}
1212 
1213 	ctx->scsi_tgt_num = scsi_tgt_num;
1214 	ctx->async_fini = false;
1215 
1216 	scsi_dev_state->remove_cb = cb_fn;
1217 	scsi_dev_state->remove_ctx = cb_arg;
1218 	scsi_dev_state->status = VHOST_SCSI_DEV_REMOVING;
1219 
1220 	vhost_user_dev_foreach_session(vdev, vhost_scsi_session_remove_tgt,
1221 				       vhost_scsi_dev_remove_tgt_cpl_cb, ctx);
1222 	return 0;
1223 }
1224 
1225 static int
1226 vhost_scsi_session_param_changed(struct spdk_vhost_dev *vdev,
1227 				 struct spdk_vhost_session *vsession, void *ctx)
1228 {
1229 	unsigned scsi_tgt_num = (unsigned)(uintptr_t)ctx;
1230 	struct spdk_vhost_scsi_session *svsession = (struct spdk_vhost_scsi_session *)vsession;
1231 	struct spdk_scsi_dev_session_state *state = &svsession->scsi_dev_state[scsi_tgt_num];
1232 
1233 	if (!vsession->started || state->dev == NULL) {
1234 		/* Nothing to do */
1235 		return 0;
1236 	}
1237 
1238 	/* Send a parameter change virtio event */
1239 	if (vhost_dev_has_feature(vsession, VIRTIO_SCSI_F_CHANGE)) {
1240 		/*
1241 		 * virtio 1.0 spec says:
1242 		 * By sending this event, the device signals a change in the configuration
1243 		 * parameters of a logical unit, for example the capacity or cache mode.
1244 		 * event is set to VIRTIO_SCSI_T_PARAM_CHANGE. lun addresses a logical unit
1245 		 * in the SCSI host. The same event SHOULD also be reported as a unit
1246 		 * attention condition. reason contains the additional sense code and
1247 		 * additional sense code qualifier, respectively in bits 0…7 and 8…15.
1248 		 * Note: For example, a change in * capacity will be reported as asc
1249 		 * 0x2a, ascq 0x09 (CAPACITY DATA HAS CHANGED).
1250 		 */
1251 		eventq_enqueue(svsession, scsi_tgt_num, VIRTIO_SCSI_T_PARAM_CHANGE, 0x2a | (0x09 << 8));
1252 	}
1253 
1254 	return 0;
1255 }
1256 
1257 static int
1258 vhost_scsi_dev_param_changed(struct spdk_vhost_dev *vdev, unsigned scsi_tgt_num)
1259 {
1260 	struct spdk_vhost_scsi_dev *svdev;
1261 	struct spdk_scsi_dev_vhost_state *scsi_dev_state;
1262 
1263 	if (scsi_tgt_num >= SPDK_VHOST_SCSI_CTRLR_MAX_DEVS) {
1264 		SPDK_ERRLOG("%s: invalid SCSI target number %d\n", vdev->name, scsi_tgt_num);
1265 		return -EINVAL;
1266 	}
1267 
1268 	svdev = to_scsi_dev(vdev);
1269 	if (!svdev) {
1270 		SPDK_ERRLOG("An invalid SCSI device that removing from a SCSI target.");
1271 		return -EINVAL;
1272 	}
1273 
1274 	scsi_dev_state = &svdev->scsi_dev_state[scsi_tgt_num];
1275 
1276 	if (scsi_dev_state->status != VHOST_SCSI_DEV_PRESENT) {
1277 		return -EBUSY;
1278 	}
1279 
1280 	if (scsi_dev_state->dev == NULL || scsi_dev_state->status == VHOST_SCSI_DEV_ADDING) {
1281 		SPDK_ERRLOG("%s: SCSI target %u is not occupied\n", vdev->name, scsi_tgt_num);
1282 		return -ENODEV;
1283 	}
1284 
1285 	assert(scsi_dev_state->status != VHOST_SCSI_DEV_EMPTY);
1286 
1287 	vhost_user_dev_foreach_session(vdev, vhost_scsi_session_param_changed,
1288 				       NULL, (void *)(uintptr_t)scsi_tgt_num);
1289 	return 0;
1290 }
1291 
1292 static void
1293 free_task_pool(struct spdk_vhost_scsi_session *svsession)
1294 {
1295 	struct spdk_vhost_session *vsession = &svsession->vsession;
1296 	struct spdk_vhost_virtqueue *vq;
1297 	uint16_t i;
1298 
1299 	for (i = 0; i < vsession->max_queues; i++) {
1300 		vq = &vsession->virtqueue[i];
1301 		if (vq->tasks == NULL) {
1302 			continue;
1303 		}
1304 
1305 		spdk_free(vq->tasks);
1306 		vq->tasks = NULL;
1307 	}
1308 }
1309 
1310 static int
1311 alloc_vq_task_pool(struct spdk_vhost_session *vsession, uint16_t qid)
1312 {
1313 	struct spdk_vhost_scsi_session *svsession = to_scsi_session(vsession);
1314 	struct spdk_vhost_virtqueue *vq;
1315 	struct spdk_vhost_scsi_task *task;
1316 	uint32_t task_cnt;
1317 	uint32_t j;
1318 
1319 	if (qid >= SPDK_VHOST_MAX_VQUEUES) {
1320 		return -EINVAL;
1321 	}
1322 
1323 	vq = &vsession->virtqueue[qid];
1324 	if (vq->vring.desc == NULL) {
1325 		return 0;
1326 	}
1327 
1328 	task_cnt = vq->vring.size;
1329 	if (task_cnt > SPDK_VHOST_MAX_VQ_SIZE) {
1330 		/* sanity check */
1331 		SPDK_ERRLOG("%s: virtqueue %"PRIu16" is too big. (size = %"PRIu32", max = %"PRIu32")\n",
1332 			    vsession->name, qid, task_cnt, SPDK_VHOST_MAX_VQ_SIZE);
1333 		return -1;
1334 	}
1335 	vq->tasks = spdk_zmalloc(sizeof(struct spdk_vhost_scsi_task) * task_cnt,
1336 				 SPDK_CACHE_LINE_SIZE, NULL,
1337 				 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
1338 	if (vq->tasks == NULL) {
1339 		SPDK_ERRLOG("%s: failed to allocate %"PRIu32" tasks for virtqueue %"PRIu16"\n",
1340 			    vsession->name, task_cnt, qid);
1341 		return -1;
1342 	}
1343 
1344 	for (j = 0; j < task_cnt; j++) {
1345 		task = &((struct spdk_vhost_scsi_task *)vq->tasks)[j];
1346 		task->svsession = svsession;
1347 		task->vq = vq;
1348 		task->req_idx = j;
1349 	}
1350 
1351 	return 0;
1352 }
1353 
1354 static int
1355 vhost_scsi_start(struct spdk_vhost_dev *vdev,
1356 		 struct spdk_vhost_session *vsession, void *unused)
1357 {
1358 	struct spdk_vhost_scsi_session *svsession = to_scsi_session(vsession);
1359 	struct spdk_vhost_scsi_dev *svdev;
1360 	struct spdk_scsi_dev_vhost_state *state;
1361 	uint32_t i;
1362 	int rc;
1363 
1364 	/* return if start is already in progress */
1365 	if (svsession->requestq_poller) {
1366 		SPDK_INFOLOG(vhost, "%s: start in progress\n", vsession->name);
1367 		return -EINPROGRESS;
1368 	}
1369 
1370 	/* validate all I/O queues are in a contiguous index range */
1371 	if (vsession->max_queues < VIRTIO_SCSI_REQUESTQ + 1) {
1372 		SPDK_INFOLOG(vhost, "%s: max_queues %u, no I/O queues\n", vsession->name, vsession->max_queues);
1373 		return -1;
1374 	}
1375 	for (i = VIRTIO_SCSI_REQUESTQ; i < vsession->max_queues; i++) {
1376 		if (vsession->virtqueue[i].vring.desc == NULL) {
1377 			SPDK_ERRLOG("%s: queue %"PRIu32" is empty\n", vsession->name, i);
1378 			return -1;
1379 		}
1380 	}
1381 
1382 	svdev = to_scsi_dev(vsession->vdev);
1383 	assert(svdev != NULL);
1384 	svsession->svdev = svdev;
1385 
1386 	for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; i++) {
1387 		state = &svdev->scsi_dev_state[i];
1388 		if (state->dev == NULL || state->status == VHOST_SCSI_DEV_REMOVING) {
1389 			continue;
1390 		}
1391 
1392 		assert(svsession->scsi_dev_state[i].status == VHOST_SCSI_DEV_EMPTY);
1393 		svsession->scsi_dev_state[i].dev = state->dev;
1394 		svsession->scsi_dev_state[i].status = VHOST_SCSI_DEV_PRESENT;
1395 		rc = spdk_scsi_dev_allocate_io_channels(state->dev);
1396 		if (rc != 0) {
1397 			SPDK_ERRLOG("%s: failed to alloc io_channel for SCSI target %"PRIu32"\n",
1398 				    vsession->name, i);
1399 			/* unset the SCSI target so that all I/O to it will be rejected */
1400 			svsession->scsi_dev_state[i].dev = NULL;
1401 			/* set EMPTY state so that we won't reply with SCSI hotremove
1402 			 * sense codes - the device hasn't ever been added.
1403 			 */
1404 			svsession->scsi_dev_state[i].status = VHOST_SCSI_DEV_EMPTY;
1405 			continue;
1406 		}
1407 	}
1408 	SPDK_INFOLOG(vhost, "%s: started poller on lcore %d\n",
1409 		     vsession->name, spdk_env_get_current_core());
1410 
1411 	svsession->requestq_poller = SPDK_POLLER_REGISTER(vdev_worker, svsession, 0);
1412 	svsession->mgmt_poller = SPDK_POLLER_REGISTER(vdev_mgmt_worker, svsession,
1413 				 MGMT_POLL_PERIOD_US);
1414 	return 0;
1415 }
1416 
1417 static int
1418 destroy_session_poller_cb(void *arg)
1419 {
1420 	struct spdk_vhost_scsi_session *svsession = arg;
1421 	struct spdk_vhost_session *vsession = &svsession->vsession;
1422 	struct spdk_scsi_dev_session_state *state;
1423 	uint32_t i;
1424 
1425 	if (vsession->task_cnt > 0 || spdk_vhost_trylock() != 0) {
1426 		assert(vsession->stop_retry_count > 0);
1427 		vsession->stop_retry_count--;
1428 		if (vsession->stop_retry_count == 0) {
1429 			SPDK_ERRLOG("%s: Timedout when destroy session (task_cnt %d)\n", vsession->name,
1430 				    vsession->task_cnt);
1431 			spdk_poller_unregister(&svsession->stop_poller);
1432 			vhost_user_session_stop_done(vsession, -ETIMEDOUT);
1433 		}
1434 
1435 		return SPDK_POLLER_BUSY;
1436 	}
1437 
1438 	for (i = 0; i < vsession->max_queues; i++) {
1439 		vhost_vq_used_signal(vsession, &vsession->virtqueue[i]);
1440 	}
1441 
1442 	for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; i++) {
1443 		enum spdk_scsi_dev_vhost_status prev_status;
1444 
1445 		state = &svsession->scsi_dev_state[i];
1446 		/* clear the REMOVED status so that we won't send hotremove events anymore */
1447 		prev_status = state->status;
1448 		state->status = VHOST_SCSI_DEV_EMPTY;
1449 		if (state->dev == NULL) {
1450 			continue;
1451 		}
1452 
1453 		spdk_scsi_dev_free_io_channels(state->dev);
1454 
1455 		state->dev = NULL;
1456 
1457 		if (prev_status == VHOST_SCSI_DEV_REMOVING) {
1458 			/* try to detach it globally */
1459 			vhost_user_dev_foreach_session(vsession->vdev,
1460 						       vhost_scsi_session_process_removed,
1461 						       vhost_scsi_dev_process_removed_cpl_cb,
1462 						       (void *)(uintptr_t)i);
1463 		}
1464 	}
1465 
1466 	SPDK_INFOLOG(vhost, "%s: stopping poller on lcore %d\n",
1467 		     vsession->name, spdk_env_get_current_core());
1468 
1469 	free_task_pool(svsession);
1470 
1471 	spdk_poller_unregister(&svsession->stop_poller);
1472 	vhost_user_session_stop_done(vsession, 0);
1473 
1474 	spdk_vhost_unlock();
1475 	return SPDK_POLLER_BUSY;
1476 }
1477 
1478 static int
1479 vhost_scsi_stop_cb(struct spdk_vhost_dev *vdev,
1480 		   struct spdk_vhost_session *vsession, void *unused)
1481 {
1482 	struct spdk_vhost_scsi_session *svsession = to_scsi_session(vsession);
1483 
1484 	/* return if stop is already in progress */
1485 	if (svsession->stop_poller) {
1486 		return -EINPROGRESS;
1487 	}
1488 
1489 	/* Stop receiving new I/O requests */
1490 	spdk_poller_unregister(&svsession->requestq_poller);
1491 
1492 	/* Stop receiving controlq requests, also stop processing the
1493 	 * asynchronous hotremove events. All the remaining events
1494 	 * will be finalized by the stop_poller below.
1495 	 */
1496 	spdk_poller_unregister(&svsession->mgmt_poller);
1497 
1498 	/* vhost_user_session_send_event timeout is 3 seconds, here set retry within 4 seconds */
1499 	svsession->vsession.stop_retry_count = 4000;
1500 
1501 	/* Wait for all pending I/Os to complete, then process all the
1502 	 * remaining hotremove events one last time.
1503 	 */
1504 	svsession->stop_poller = SPDK_POLLER_REGISTER(destroy_session_poller_cb,
1505 				 svsession, 1000);
1506 
1507 	return 0;
1508 }
1509 
1510 static int
1511 vhost_scsi_stop(struct spdk_vhost_session *vsession)
1512 {
1513 	return vhost_user_session_send_event(vsession, vhost_scsi_stop_cb,
1514 					     3, "stop session");
1515 }
1516 
1517 static void
1518 vhost_scsi_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
1519 {
1520 	struct spdk_scsi_dev *sdev;
1521 	struct spdk_scsi_lun *lun;
1522 	uint32_t dev_idx;
1523 
1524 	assert(vdev != NULL);
1525 	spdk_json_write_named_array_begin(w, "scsi");
1526 	for (dev_idx = 0; dev_idx < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; dev_idx++) {
1527 		sdev = spdk_vhost_scsi_dev_get_tgt(vdev, dev_idx);
1528 		if (!sdev) {
1529 			continue;
1530 		}
1531 
1532 		spdk_json_write_object_begin(w);
1533 
1534 		spdk_json_write_named_uint32(w, "scsi_dev_num", dev_idx);
1535 
1536 		spdk_json_write_named_uint32(w, "id", spdk_scsi_dev_get_id(sdev));
1537 
1538 		spdk_json_write_named_string(w, "target_name", spdk_scsi_dev_get_name(sdev));
1539 
1540 		spdk_json_write_named_array_begin(w, "luns");
1541 
1542 		for (lun = spdk_scsi_dev_get_first_lun(sdev); lun != NULL;
1543 		     lun = spdk_scsi_dev_get_next_lun(lun)) {
1544 			spdk_json_write_object_begin(w);
1545 
1546 			spdk_json_write_named_int32(w, "id", spdk_scsi_lun_get_id(lun));
1547 
1548 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1549 
1550 			spdk_json_write_object_end(w);
1551 		}
1552 
1553 		spdk_json_write_array_end(w);
1554 		spdk_json_write_object_end(w);
1555 	}
1556 
1557 	spdk_json_write_array_end(w);
1558 }
1559 
1560 static void
1561 vhost_scsi_write_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
1562 {
1563 	struct spdk_scsi_dev *scsi_dev;
1564 	struct spdk_scsi_lun *lun;
1565 	uint32_t i;
1566 
1567 	spdk_json_write_object_begin(w);
1568 	spdk_json_write_named_string(w, "method", "vhost_create_scsi_controller");
1569 
1570 	spdk_json_write_named_object_begin(w, "params");
1571 	spdk_json_write_named_string(w, "ctrlr", vdev->name);
1572 	spdk_json_write_named_string(w, "cpumask",
1573 				     spdk_cpuset_fmt(spdk_thread_get_cpumask(vdev->thread)));
1574 	spdk_json_write_object_end(w);
1575 
1576 	spdk_json_write_object_end(w);
1577 
1578 	for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; i++) {
1579 		scsi_dev = spdk_vhost_scsi_dev_get_tgt(vdev, i);
1580 		if (scsi_dev == NULL) {
1581 			continue;
1582 		}
1583 
1584 		lun = spdk_scsi_dev_get_lun(scsi_dev, 0);
1585 		assert(lun != NULL);
1586 
1587 		spdk_json_write_object_begin(w);
1588 		spdk_json_write_named_string(w, "method", "vhost_scsi_controller_add_target");
1589 
1590 		spdk_json_write_named_object_begin(w, "params");
1591 		spdk_json_write_named_string(w, "ctrlr", vdev->name);
1592 		spdk_json_write_named_uint32(w, "scsi_target_num", i);
1593 
1594 		spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1595 		spdk_json_write_object_end(w);
1596 
1597 		spdk_json_write_object_end(w);
1598 	}
1599 }
1600 
1601 SPDK_LOG_REGISTER_COMPONENT(vhost_scsi)
1602 SPDK_LOG_REGISTER_COMPONENT(vhost_scsi_queue)
1603 SPDK_LOG_REGISTER_COMPONENT(vhost_scsi_data)
1604