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