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