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