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