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