blob: 5340fd759c6e05010437cc0a85f52c4222ee349b [file] [log] [blame]
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001/*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
3 *
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
6 *
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 *
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <generated/utsrelease.h>
27#include <linux/utsname.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/kthread.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/configfs.h>
34#include <linux/ctype.h>
35#include <linux/compat.h>
36#include <linux/eventfd.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070037#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <asm/unaligned.h>
40#include <scsi/scsi.h>
41#include <scsi/scsi_tcq.h>
42#include <target/target_core_base.h>
43#include <target/target_core_fabric.h>
44#include <target/target_core_fabric_configfs.h>
45#include <target/target_core_configfs.h>
46#include <target/configfs_macros.h>
47#include <linux/vhost.h>
48#include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
49#include <linux/virtio_scsi.h>
Asias He9d6064a2013-01-06 14:36:13 +080050#include <linux/llist.h>
Asias He1b7f3902013-02-06 13:20:59 +080051#include <linux/bitmap.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070052
53#include "vhost.c"
54#include "vhost.h"
55#include "tcm_vhost.h"
56
Nicholas Bellinger101998f2012-07-30 13:30:00 -070057enum {
58 VHOST_SCSI_VQ_CTL = 0,
59 VHOST_SCSI_VQ_EVT = 1,
60 VHOST_SCSI_VQ_IO = 2,
61};
62
Nicholas Bellinger5dade712013-03-27 17:23:41 -070063/*
64 * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in
65 * kernel but disabling it helps.
66 * TODO: debug and remove the workaround.
67 */
68enum {
69 VHOST_SCSI_FEATURES = VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)
70};
71
Asias He1b7f3902013-02-06 13:20:59 +080072#define VHOST_SCSI_MAX_TARGET 256
73#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +080074#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +080075
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070076struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +080077 /* Protected by vhost_scsi->dev.mutex */
Asias He4f7f46d2013-04-03 14:17:37 +080078 struct tcm_vhost_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +080079 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +080080
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070081 struct vhost_dev dev;
Asias He1b7f3902013-02-06 13:20:59 +080082 struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070083
84 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +080085 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +080086
87 struct vhost_work vs_event_work; /* evt injection work item */
88 struct llist_head vs_event_list; /* evt injection queue */
89
90 bool vs_events_missed; /* any missed events, protected by vq->mutex */
91 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070092};
93
94/* Local pointer to allocated TCM configfs fabric module */
95static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
96
97static struct workqueue_struct *tcm_vhost_workqueue;
98
99/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
100static DEFINE_MUTEX(tcm_vhost_mutex);
101static LIST_HEAD(tcm_vhost_list);
102
Asias He765b34f2013-01-22 11:20:25 +0800103static int iov_num_pages(struct iovec *iov)
104{
105 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
106 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
107}
108
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700109static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
110{
111 return 1;
112}
113
114static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
115{
116 return 0;
117}
118
119static char *tcm_vhost_get_fabric_name(void)
120{
121 return "vhost";
122}
123
124static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
125{
126 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
127 struct tcm_vhost_tpg, se_tpg);
128 struct tcm_vhost_tport *tport = tpg->tport;
129
130 switch (tport->tport_proto_id) {
131 case SCSI_PROTOCOL_SAS:
132 return sas_get_fabric_proto_ident(se_tpg);
133 case SCSI_PROTOCOL_FCP:
134 return fc_get_fabric_proto_ident(se_tpg);
135 case SCSI_PROTOCOL_ISCSI:
136 return iscsi_get_fabric_proto_ident(se_tpg);
137 default:
138 pr_err("Unknown tport_proto_id: 0x%02x, using"
139 " SAS emulation\n", tport->tport_proto_id);
140 break;
141 }
142
143 return sas_get_fabric_proto_ident(se_tpg);
144}
145
146static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
147{
148 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
149 struct tcm_vhost_tpg, se_tpg);
150 struct tcm_vhost_tport *tport = tpg->tport;
151
152 return &tport->tport_name[0];
153}
154
155static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
156{
157 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
158 struct tcm_vhost_tpg, se_tpg);
159 return tpg->tport_tpgt;
160}
161
162static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
163{
164 return 1;
165}
166
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700167static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700168 struct se_node_acl *se_nacl,
169 struct t10_pr_registration *pr_reg,
170 int *format_code,
171 unsigned char *buf)
172{
173 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
174 struct tcm_vhost_tpg, se_tpg);
175 struct tcm_vhost_tport *tport = tpg->tport;
176
177 switch (tport->tport_proto_id) {
178 case SCSI_PROTOCOL_SAS:
179 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
180 format_code, buf);
181 case SCSI_PROTOCOL_FCP:
182 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
183 format_code, buf);
184 case SCSI_PROTOCOL_ISCSI:
185 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
186 format_code, buf);
187 default:
188 pr_err("Unknown tport_proto_id: 0x%02x, using"
189 " SAS emulation\n", tport->tport_proto_id);
190 break;
191 }
192
193 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
194 format_code, buf);
195}
196
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700197static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700198 struct se_node_acl *se_nacl,
199 struct t10_pr_registration *pr_reg,
200 int *format_code)
201{
202 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
203 struct tcm_vhost_tpg, se_tpg);
204 struct tcm_vhost_tport *tport = tpg->tport;
205
206 switch (tport->tport_proto_id) {
207 case SCSI_PROTOCOL_SAS:
208 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
209 format_code);
210 case SCSI_PROTOCOL_FCP:
211 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
212 format_code);
213 case SCSI_PROTOCOL_ISCSI:
214 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
215 format_code);
216 default:
217 pr_err("Unknown tport_proto_id: 0x%02x, using"
218 " SAS emulation\n", tport->tport_proto_id);
219 break;
220 }
221
222 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
223 format_code);
224}
225
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700226static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700227 const char *buf,
228 u32 *out_tid_len,
229 char **port_nexus_ptr)
230{
231 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
232 struct tcm_vhost_tpg, se_tpg);
233 struct tcm_vhost_tport *tport = tpg->tport;
234
235 switch (tport->tport_proto_id) {
236 case SCSI_PROTOCOL_SAS:
237 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
238 port_nexus_ptr);
239 case SCSI_PROTOCOL_FCP:
240 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
241 port_nexus_ptr);
242 case SCSI_PROTOCOL_ISCSI:
243 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
244 port_nexus_ptr);
245 default:
246 pr_err("Unknown tport_proto_id: 0x%02x, using"
247 " SAS emulation\n", tport->tport_proto_id);
248 break;
249 }
250
251 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
252 port_nexus_ptr);
253}
254
255static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
256 struct se_portal_group *se_tpg)
257{
258 struct tcm_vhost_nacl *nacl;
259
260 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
261 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900262 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700263 return NULL;
264 }
265
266 return &nacl->se_node_acl;
267}
268
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700269static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700270 struct se_node_acl *se_nacl)
271{
272 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
273 struct tcm_vhost_nacl, se_node_acl);
274 kfree(nacl);
275}
276
277static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
278{
279 return 1;
280}
281
282static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
283{
284 return;
285}
286
287static int tcm_vhost_shutdown_session(struct se_session *se_sess)
288{
289 return 0;
290}
291
292static void tcm_vhost_close_session(struct se_session *se_sess)
293{
294 return;
295}
296
297static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
298{
299 return 0;
300}
301
302static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
303{
304 /* Go ahead and process the write immediately */
305 target_execute_cmd(se_cmd);
306 return 0;
307}
308
309static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
310{
311 return 0;
312}
313
314static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
315{
316 return;
317}
318
319static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
320{
321 return 0;
322}
323
324static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
325{
326 return 0;
327}
328
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700329static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
330{
331 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
332
Asias He9d6064a2013-01-06 14:36:13 +0800333 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700334
335 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
336}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700337
338static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
339{
340 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
341 struct tcm_vhost_cmd, tvc_se_cmd);
342 vhost_scsi_complete_cmd(tv_cmd);
343 return 0;
344}
345
346static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
347{
348 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
349 struct tcm_vhost_cmd, tvc_se_cmd);
350 vhost_scsi_complete_cmd(tv_cmd);
351 return 0;
352}
353
354static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
355{
356 return 0;
357}
358
Asias Hea6c9af82013-04-25 15:35:21 +0800359static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
360{
361 vs->vs_events_nr--;
362 kfree(evt);
363}
364
365static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
366 u32 event, u32 reason)
367{
368 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
369 struct tcm_vhost_evt *evt;
370
371 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
372 vs->vs_events_missed = true;
373 return NULL;
374 }
375
376 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
377 if (!evt) {
378 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
379 vs->vs_events_missed = true;
380 return NULL;
381 }
382
383 evt->event.event = event;
384 evt->event.reason = reason;
385 vs->vs_events_nr++;
386
387 return evt;
388}
389
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700390static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
391{
392 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
393
394 /* TODO locking against target/backend threads? */
395 transport_generic_free_cmd(se_cmd, 1);
396
397 if (tv_cmd->tvc_sgl_count) {
398 u32 i;
399 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
400 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
401
402 kfree(tv_cmd->tvc_sgl);
403 }
404
405 kfree(tv_cmd);
406}
407
Asias Hea6c9af82013-04-25 15:35:21 +0800408static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
409 struct tcm_vhost_evt *evt)
410{
411 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
412 struct virtio_scsi_event *event = &evt->event;
413 struct virtio_scsi_event __user *eventp;
414 unsigned out, in;
415 int head, ret;
416
417 if (!vq->private_data) {
418 vs->vs_events_missed = true;
419 return;
420 }
421
422again:
423 vhost_disable_notify(&vs->dev, vq);
424 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
425 ARRAY_SIZE(vq->iov), &out, &in,
426 NULL, NULL);
427 if (head < 0) {
428 vs->vs_events_missed = true;
429 return;
430 }
431 if (head == vq->num) {
432 if (vhost_enable_notify(&vs->dev, vq))
433 goto again;
434 vs->vs_events_missed = true;
435 return;
436 }
437
438 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
439 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
440 vq->iov[out].iov_len);
441 vs->vs_events_missed = true;
442 return;
443 }
444
445 if (vs->vs_events_missed) {
446 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
447 vs->vs_events_missed = false;
448 }
449
450 eventp = vq->iov[out].iov_base;
451 ret = __copy_to_user(eventp, event, sizeof(*event));
452 if (!ret)
453 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
454 else
455 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
456}
457
458static void tcm_vhost_evt_work(struct vhost_work *work)
459{
460 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
461 vs_event_work);
462 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
463 struct tcm_vhost_evt *evt;
464 struct llist_node *llnode;
465
466 mutex_lock(&vq->mutex);
467 llnode = llist_del_all(&vs->vs_event_list);
468 while (llnode) {
469 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
470 llnode = llist_next(llnode);
471 tcm_vhost_do_evt_work(vs, evt);
472 tcm_vhost_free_evt(vs, evt);
473 }
474 mutex_unlock(&vq->mutex);
475}
476
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700477/* Fill in status and signal that we are done processing this command
478 *
479 * This is scheduled in the vhost work queue so we are called with the owner
480 * process mm and can access the vring.
481 */
482static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
483{
484 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
485 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800486 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800487 struct virtio_scsi_cmd_resp v_rsp;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700488 struct tcm_vhost_cmd *tv_cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800489 struct llist_node *llnode;
490 struct se_cmd *se_cmd;
Asias He1b7f3902013-02-06 13:20:59 +0800491 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700492
Asias He1b7f3902013-02-06 13:20:59 +0800493 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800494 llnode = llist_del_all(&vs->vs_completion_list);
495 while (llnode) {
496 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
497 tvc_completion_list);
498 llnode = llist_next(llnode);
499 se_cmd = &tv_cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700500
501 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
502 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
503
504 memset(&v_rsp, 0, sizeof(v_rsp));
505 v_rsp.resid = se_cmd->residual_count;
506 /* TODO is status_qualifier field needed? */
507 v_rsp.status = se_cmd->scsi_status;
508 v_rsp.sense_len = se_cmd->scsi_sense_length;
509 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
510 v_rsp.sense_len);
511 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
Asias He1b7f3902013-02-06 13:20:59 +0800512 if (likely(ret == 0)) {
513 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
514 vq = tv_cmd->tvc_vq - vs->vqs;
515 __set_bit(vq, signal);
516 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700517 pr_err("Faulted on virtio_scsi_cmd_resp\n");
518
519 vhost_scsi_free_cmd(tv_cmd);
520 }
521
Asias He1b7f3902013-02-06 13:20:59 +0800522 vq = -1;
523 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
524 < VHOST_SCSI_MAX_VQ)
525 vhost_signal(&vs->dev, &vs->vqs[vq]);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700526}
527
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700528static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
529 struct tcm_vhost_tpg *tv_tpg,
530 struct virtio_scsi_cmd_req *v_req,
531 u32 exp_data_len,
532 int data_direction)
533{
534 struct tcm_vhost_cmd *tv_cmd;
535 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700536
537 tv_nexus = tv_tpg->tpg_nexus;
538 if (!tv_nexus) {
539 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
540 return ERR_PTR(-EIO);
541 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700542
543 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
544 if (!tv_cmd) {
545 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
546 return ERR_PTR(-ENOMEM);
547 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700548 tv_cmd->tvc_tag = v_req->tag;
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700549 tv_cmd->tvc_task_attr = v_req->task_attr;
550 tv_cmd->tvc_exp_data_len = exp_data_len;
551 tv_cmd->tvc_data_direction = data_direction;
552 tv_cmd->tvc_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700553
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700554 return tv_cmd;
555}
556
557/*
558 * Map a user memory range into a scatterlist
559 *
560 * Returns the number of scatterlist entries used or -errno on error.
561 */
562static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
Asias He18100532013-01-22 11:20:27 +0800563 unsigned int sgl_count, struct iovec *iov, int write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700564{
Asias He18100532013-01-22 11:20:27 +0800565 unsigned int npages = 0, pages_nr, offset, nbytes;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700566 struct scatterlist *sg = sgl;
Asias He18100532013-01-22 11:20:27 +0800567 void __user *ptr = iov->iov_base;
568 size_t len = iov->iov_len;
569 struct page **pages;
570 int ret, i;
571
572 pages_nr = iov_num_pages(iov);
573 if (pages_nr > sgl_count)
574 return -ENOBUFS;
575
576 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
577 if (!pages)
578 return -ENOMEM;
579
580 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
581 /* No pages were pinned */
582 if (ret < 0)
583 goto out;
584 /* Less pages pinned than wanted */
585 if (ret != pages_nr) {
586 for (i = 0; i < ret; i++)
587 put_page(pages[i]);
588 ret = -EFAULT;
589 goto out;
590 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700591
592 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800593 offset = (uintptr_t)ptr & ~PAGE_MASK;
594 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
595 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700596 ptr += nbytes;
597 len -= nbytes;
598 sg++;
599 npages++;
600 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700601
Asias He18100532013-01-22 11:20:27 +0800602out:
603 kfree(pages);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700604 return ret;
605}
606
607static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
608 struct iovec *iov, unsigned int niov, int write)
609{
610 int ret;
611 unsigned int i;
612 u32 sgl_count;
613 struct scatterlist *sg;
614
615 /*
616 * Find out how long sglist needs to be
617 */
618 sgl_count = 0;
Asias Hef3158f32013-01-22 11:20:26 +0800619 for (i = 0; i < niov; i++)
620 sgl_count += iov_num_pages(&iov[i]);
621
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700622 /* TODO overflow checking */
623
624 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
625 if (!sg)
626 return -ENOMEM;
Fengguang Wuf0e0e9b2012-07-30 13:19:07 -0700627 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
628 sg, sgl_count, !sg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700629 sg_init_table(sg, sgl_count);
630
631 tv_cmd->tvc_sgl = sg;
632 tv_cmd->tvc_sgl_count = sgl_count;
633
634 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
635 for (i = 0; i < niov; i++) {
Asias He18100532013-01-22 11:20:27 +0800636 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700637 if (ret < 0) {
638 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
639 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
640 kfree(tv_cmd->tvc_sgl);
641 tv_cmd->tvc_sgl = NULL;
642 tv_cmd->tvc_sgl_count = 0;
643 return ret;
644 }
645
646 sg += ret;
647 sgl_count -= ret;
648 }
649 return 0;
650}
651
652static void tcm_vhost_submission_work(struct work_struct *work)
653{
654 struct tcm_vhost_cmd *tv_cmd =
655 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700656 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700657 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
658 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
659 int rc, sg_no_bidi = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700660
661 if (tv_cmd->tvc_sgl_count) {
662 sg_ptr = tv_cmd->tvc_sgl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700663/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
664#if 0
665 if (se_cmd->se_cmd_flags & SCF_BIDI) {
666 sg_bidi_ptr = NULL;
667 sg_no_bidi = 0;
668 }
669#endif
670 } else {
671 sg_ptr = NULL;
672 }
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700673 tv_nexus = tv_cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700674
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700675 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
676 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
677 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
678 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
679 0, sg_ptr, tv_cmd->tvc_sgl_count,
680 sg_bidi_ptr, sg_no_bidi);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700681 if (rc < 0) {
682 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700683 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700684 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700685 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700686}
687
Asias He637ab212013-04-10 15:06:15 +0800688static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
689 struct vhost_virtqueue *vq, int head, unsigned out)
690{
691 struct virtio_scsi_cmd_resp __user *resp;
692 struct virtio_scsi_cmd_resp rsp;
693 int ret;
694
695 memset(&rsp, 0, sizeof(rsp));
696 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
697 resp = vq->iov[out].iov_base;
698 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
699 if (!ret)
700 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
701 else
702 pr_err("Faulted on virtio_scsi_cmd_resp\n");
703}
704
Asias He1b7f3902013-02-06 13:20:59 +0800705static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
706 struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700707{
Asias He4f7f46d2013-04-03 14:17:37 +0800708 struct tcm_vhost_tpg **vs_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700709 struct virtio_scsi_cmd_req v_req;
710 struct tcm_vhost_tpg *tv_tpg;
711 struct tcm_vhost_cmd *tv_cmd;
712 u32 exp_data_len, data_first, data_num, data_direction;
713 unsigned out, in, i;
714 int head, ret;
Asias He67e18cf2013-02-05 12:31:57 +0800715 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700716
Asias He4f7f46d2013-04-03 14:17:37 +0800717 /*
718 * We can handle the vq only after the endpoint is setup by calling the
719 * VHOST_SCSI_SET_ENDPOINT ioctl.
720 *
721 * TODO: Check that we are running from vhost_worker which acts
722 * as read-side critical section for vhost kind of RCU.
723 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
724 */
725 vs_tpg = rcu_dereference_check(vq->private_data, 1);
726 if (!vs_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700727 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700728
729 mutex_lock(&vq->mutex);
730 vhost_disable_notify(&vs->dev, vq);
731
732 for (;;) {
733 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
734 ARRAY_SIZE(vq->iov), &out, &in,
735 NULL, NULL);
736 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
737 head, out, in);
738 /* On error, stop handling until the next kick. */
739 if (unlikely(head < 0))
740 break;
741 /* Nothing new? Wait for eventfd to tell us they refilled. */
742 if (head == vq->num) {
743 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
744 vhost_disable_notify(&vs->dev, vq);
745 continue;
746 }
747 break;
748 }
749
750/* FIXME: BIDI operation */
751 if (out == 1 && in == 1) {
752 data_direction = DMA_NONE;
753 data_first = 0;
754 data_num = 0;
755 } else if (out == 1 && in > 1) {
756 data_direction = DMA_FROM_DEVICE;
757 data_first = out + 1;
758 data_num = in - 1;
759 } else if (out > 1 && in == 1) {
760 data_direction = DMA_TO_DEVICE;
761 data_first = 1;
762 data_num = out - 1;
763 } else {
764 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
765 out, in);
766 break;
767 }
768
769 /*
770 * Check for a sane resp buffer so we can report errors to
771 * the guest.
772 */
773 if (unlikely(vq->iov[out].iov_len !=
774 sizeof(struct virtio_scsi_cmd_resp))) {
775 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
776 " bytes\n", vq->iov[out].iov_len);
777 break;
778 }
779
780 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
781 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
782 " bytes\n", vq->iov[0].iov_len);
783 break;
784 }
785 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
786 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
787 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
788 sizeof(v_req));
789 if (unlikely(ret)) {
790 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
791 break;
792 }
793
Asias He67e18cf2013-02-05 12:31:57 +0800794 /* Extract the tpgt */
795 target = v_req.lun[1];
Asias He4f7f46d2013-04-03 14:17:37 +0800796 tv_tpg = ACCESS_ONCE(vs_tpg[target]);
Asias He67e18cf2013-02-05 12:31:57 +0800797
798 /* Target does not exist, fail the request */
799 if (unlikely(!tv_tpg)) {
Asias He637ab212013-04-10 15:06:15 +0800800 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +0800801 continue;
802 }
803
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700804 exp_data_len = 0;
805 for (i = 0; i < data_num; i++)
806 exp_data_len += vq->iov[data_first + i].iov_len;
807
808 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
809 exp_data_len, data_direction);
810 if (IS_ERR(tv_cmd)) {
811 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
812 PTR_ERR(tv_cmd));
Asias He055f6482013-04-10 15:06:16 +0800813 goto err_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700814 }
815 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
816 ": %d\n", tv_cmd, exp_data_len, data_direction);
817
818 tv_cmd->tvc_vhost = vs;
Asias He1b7f3902013-02-06 13:20:59 +0800819 tv_cmd->tvc_vq = vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700820 tv_cmd->tvc_resp = vq->iov[out].iov_base;
821
822 /*
823 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
824 * that will be used by tcm_vhost_new_cmd_map() and down into
825 * target_setup_cmd_from_cdb()
826 */
827 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
828 /*
829 * Check that the recieved CDB size does not exceeded our
830 * hardcoded max for tcm_vhost
831 */
832 /* TODO what if cdb was too small for varlen cdb header? */
833 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
834 TCM_VHOST_MAX_CDB_SIZE)) {
835 vq_err(vq, "Received SCSI CDB with command_size: %d that"
836 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
837 scsi_command_size(tv_cmd->tvc_cdb),
838 TCM_VHOST_MAX_CDB_SIZE);
Asias He055f6482013-04-10 15:06:16 +0800839 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700840 }
841 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
842
843 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
844 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
845
846 if (data_direction != DMA_NONE) {
847 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
848 &vq->iov[data_first], data_num,
849 data_direction == DMA_TO_DEVICE);
850 if (unlikely(ret)) {
851 vq_err(vq, "Failed to map iov to sgl\n");
Asias He055f6482013-04-10 15:06:16 +0800852 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700853 }
854 }
855
856 /*
857 * Save the descriptor from vhost_get_vq_desc() to be used to
858 * complete the virtio-scsi request in TCM callback context via
859 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
860 */
861 tv_cmd->tvc_vq_desc = head;
862 /*
863 * Dispatch tv_cmd descriptor for cmwq execution in process
864 * context provided by tcm_vhost_workqueue. This also ensures
865 * tv_cmd is executed on the same kworker CPU as this vhost
866 * thread to gain positive L2 cache locality effects..
867 */
868 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
869 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
870 }
871
872 mutex_unlock(&vq->mutex);
Asias He7ea206c2013-04-10 15:06:14 +0800873 return;
874
Asias He055f6482013-04-10 15:06:16 +0800875err_free:
Asias He7ea206c2013-04-10 15:06:14 +0800876 vhost_scsi_free_cmd(tv_cmd);
Asias He055f6482013-04-10 15:06:16 +0800877err_cmd:
878 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He7ea206c2013-04-10 15:06:14 +0800879 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700880}
881
882static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
883{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700884 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700885}
886
Asias Hea6c9af82013-04-25 15:35:21 +0800887static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
888 struct se_lun *lun, u32 event, u32 reason)
889{
890 struct tcm_vhost_evt *evt;
891
892 evt = tcm_vhost_allocate_evt(vs, event, reason);
893 if (!evt)
894 return;
895
896 if (tpg && lun) {
897 /* TODO: share lun setup code with virtio-scsi.ko */
898 /*
899 * Note: evt->event is zeroed when we allocate it and
900 * lun[4-7] need to be zero according to virtio-scsi spec.
901 */
902 evt->event.lun[0] = 0x01;
903 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
904 if (lun->unpacked_lun >= 256)
905 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
906 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
907 }
908
909 llist_add(&evt->list, &vs->vs_event_list);
910 vhost_work_queue(&vs->dev, &vs->vs_event_work);
911}
912
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700913static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
914{
Asias Hea6c9af82013-04-25 15:35:21 +0800915 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
916 poll.work);
917 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
918
919 mutex_lock(&vq->mutex);
920 if (!vq->private_data)
921 goto out;
922
923 if (vs->vs_events_missed)
924 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
925out:
926 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700927}
928
929static void vhost_scsi_handle_kick(struct vhost_work *work)
930{
931 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
932 poll.work);
933 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
934
Asias He1b7f3902013-02-06 13:20:59 +0800935 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700936}
937
Asias He4f7f46d2013-04-03 14:17:37 +0800938static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
939{
940 vhost_poll_flush(&vs->dev.vqs[index].poll);
941}
942
943static void vhost_scsi_flush(struct vhost_scsi *vs)
944{
945 int i;
946
947 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
948 vhost_scsi_flush_vq(vs, i);
949 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +0800950 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias He4f7f46d2013-04-03 14:17:37 +0800951}
952
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700953/*
954 * Called from vhost_scsi_ioctl() context to walk the list of available
955 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +0800956 *
957 * The lock nesting rule is:
958 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700959 */
960static int vhost_scsi_set_endpoint(
961 struct vhost_scsi *vs,
962 struct vhost_scsi_target *t)
963{
964 struct tcm_vhost_tport *tv_tport;
965 struct tcm_vhost_tpg *tv_tpg;
Asias He4f7f46d2013-04-03 14:17:37 +0800966 struct tcm_vhost_tpg **vs_tpg;
967 struct vhost_virtqueue *vq;
968 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +0800969 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700970
Asias Hef2b7daf2013-04-25 15:35:20 +0800971 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700972 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +0800973
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700974 /* Verify that ring has been setup correctly. */
975 for (index = 0; index < vs->dev.nvqs; ++index) {
976 /* Verify that ring has been setup correctly. */
977 if (!vhost_vq_access_ok(&vs->vqs[index])) {
Asias Hef2b7daf2013-04-25 15:35:20 +0800978 ret = -EFAULT;
979 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700980 }
981 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700982
Asias He4f7f46d2013-04-03 14:17:37 +0800983 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
984 vs_tpg = kzalloc(len, GFP_KERNEL);
985 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +0800986 ret = -ENOMEM;
987 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +0800988 }
989 if (vs->vs_tpg)
990 memcpy(vs_tpg, vs->vs_tpg, len);
991
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700992 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
993 mutex_lock(&tv_tpg->tv_tpg_mutex);
994 if (!tv_tpg->tpg_nexus) {
995 mutex_unlock(&tv_tpg->tv_tpg_mutex);
996 continue;
997 }
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700998 if (tv_tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700999 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1000 continue;
1001 }
1002 tv_tport = tv_tpg->tport;
1003
Asias He67e18cf2013-02-05 12:31:57 +08001004 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He4f7f46d2013-04-03 14:17:37 +08001005 if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001006 kfree(vs_tpg);
Asias Hef2b7daf2013-04-25 15:35:20 +08001007 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1008 ret = -EEXIST;
1009 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001010 }
Asias He67e18cf2013-02-05 12:31:57 +08001011 tv_tpg->tv_tpg_vhost_count++;
Asias Hea6c9af82013-04-25 15:35:21 +08001012 tv_tpg->vhost_scsi = vs;
Asias He4f7f46d2013-04-03 14:17:37 +08001013 vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001014 smp_mb__after_atomic_inc();
Asias He67e18cf2013-02-05 12:31:57 +08001015 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001016 }
1017 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1018 }
Asias He67e18cf2013-02-05 12:31:57 +08001019
1020 if (match) {
1021 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1022 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001023 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1024 vq = &vs->vqs[i];
1025 /* Flushing the vhost_work acts as synchronize_rcu */
1026 mutex_lock(&vq->mutex);
1027 rcu_assign_pointer(vq->private_data, vs_tpg);
Asias Hedfd5d562013-04-03 14:17:38 +08001028 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001029 mutex_unlock(&vq->mutex);
1030 }
Asias He67e18cf2013-02-05 12:31:57 +08001031 ret = 0;
1032 } else {
1033 ret = -EEXIST;
1034 }
1035
Asias He4f7f46d2013-04-03 14:17:37 +08001036 /*
1037 * Act as synchronize_rcu to make sure access to
1038 * old vs->vs_tpg is finished.
1039 */
1040 vhost_scsi_flush(vs);
1041 kfree(vs->vs_tpg);
1042 vs->vs_tpg = vs_tpg;
1043
Asias Hef2b7daf2013-04-25 15:35:20 +08001044out:
Asias He67e18cf2013-02-05 12:31:57 +08001045 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001046 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001047 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001048}
1049
1050static int vhost_scsi_clear_endpoint(
1051 struct vhost_scsi *vs,
1052 struct vhost_scsi_target *t)
1053{
1054 struct tcm_vhost_tport *tv_tport;
1055 struct tcm_vhost_tpg *tv_tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001056 struct vhost_virtqueue *vq;
1057 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001058 int index, ret, i;
1059 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001060
Asias Hef2b7daf2013-04-25 15:35:20 +08001061 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001062 mutex_lock(&vs->dev.mutex);
1063 /* Verify that ring has been setup correctly. */
1064 for (index = 0; index < vs->dev.nvqs; ++index) {
1065 if (!vhost_vq_access_ok(&vs->vqs[index])) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001066 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +08001067 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001068 }
1069 }
Asias He4f7f46d2013-04-03 14:17:37 +08001070
1071 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001072 ret = 0;
1073 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001074 }
1075
Asias He67e18cf2013-02-05 12:31:57 +08001076 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1077 target = i;
Asias He67e18cf2013-02-05 12:31:57 +08001078 tv_tpg = vs->vs_tpg[target];
1079 if (!tv_tpg)
1080 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001081
Asias He038e0af2013-03-15 09:14:05 +08001082 mutex_lock(&tv_tpg->tv_tpg_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001083 tv_tport = tv_tpg->tport;
1084 if (!tv_tport) {
1085 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +08001086 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001087 }
1088
1089 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1090 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
1091 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1092 tv_tport->tport_name, tv_tpg->tport_tpgt,
1093 t->vhost_wwpn, t->vhost_tpgt);
1094 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +08001095 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001096 }
1097 tv_tpg->tv_tpg_vhost_count--;
Asias Hea6c9af82013-04-25 15:35:21 +08001098 tv_tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001099 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001100 match = true;
Asias He038e0af2013-03-15 09:14:05 +08001101 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001102 }
Asias He4f7f46d2013-04-03 14:17:37 +08001103 if (match) {
1104 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1105 vq = &vs->vqs[i];
1106 /* Flushing the vhost_work acts as synchronize_rcu */
1107 mutex_lock(&vq->mutex);
1108 rcu_assign_pointer(vq->private_data, NULL);
1109 mutex_unlock(&vq->mutex);
1110 }
1111 }
1112 /*
1113 * Act as synchronize_rcu to make sure access to
1114 * old vs->vs_tpg is finished.
1115 */
1116 vhost_scsi_flush(vs);
1117 kfree(vs->vs_tpg);
1118 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001119 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001120 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001121 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001122 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001123
Asias He038e0af2013-03-15 09:14:05 +08001124err_tpg:
1125 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1126err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001127 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001128 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001129 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001130}
1131
Asias He4f7f46d2013-04-03 14:17:37 +08001132static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1133{
1134 if (features & ~VHOST_SCSI_FEATURES)
1135 return -EOPNOTSUPP;
1136
1137 mutex_lock(&vs->dev.mutex);
1138 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1139 !vhost_log_access_ok(&vs->dev)) {
1140 mutex_unlock(&vs->dev.mutex);
1141 return -EFAULT;
1142 }
1143 vs->dev.acked_features = features;
1144 smp_wmb();
1145 vhost_scsi_flush(vs);
1146 mutex_unlock(&vs->dev.mutex);
1147 return 0;
1148}
1149
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001150static int vhost_scsi_open(struct inode *inode, struct file *f)
1151{
1152 struct vhost_scsi *s;
Asias He1b7f3902013-02-06 13:20:59 +08001153 int r, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001154
1155 s = kzalloc(sizeof(*s), GFP_KERNEL);
1156 if (!s)
1157 return -ENOMEM;
1158
1159 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001160 vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work);
1161
1162 s->vs_events_nr = 0;
1163 s->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001164
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001165 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
1166 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
Asias He1b7f3902013-02-06 13:20:59 +08001167 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++)
1168 s->vqs[i].handle_kick = vhost_scsi_handle_kick;
1169 r = vhost_dev_init(&s->dev, s->vqs, VHOST_SCSI_MAX_VQ);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001170 if (r < 0) {
1171 kfree(s);
1172 return r;
1173 }
1174
1175 f->private_data = s;
1176 return 0;
1177}
1178
1179static int vhost_scsi_release(struct inode *inode, struct file *f)
1180{
1181 struct vhost_scsi *s = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001182 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001183
Asias He67e18cf2013-02-05 12:31:57 +08001184 mutex_lock(&s->dev.mutex);
1185 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1186 mutex_unlock(&s->dev.mutex);
1187 vhost_scsi_clear_endpoint(s, &t);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +00001188 vhost_dev_stop(&s->dev);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001189 vhost_dev_cleanup(&s->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001190 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1191 vhost_scsi_flush(s);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001192 kfree(s);
1193 return 0;
1194}
1195
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001196static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
1197 unsigned long arg)
1198{
1199 struct vhost_scsi *vs = f->private_data;
1200 struct vhost_scsi_target backend;
1201 void __user *argp = (void __user *)arg;
1202 u64 __user *featurep = argp;
1203 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001204 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001205
1206 switch (ioctl) {
1207 case VHOST_SCSI_SET_ENDPOINT:
1208 if (copy_from_user(&backend, argp, sizeof backend))
1209 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001210 if (backend.reserved != 0)
1211 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001212
1213 return vhost_scsi_set_endpoint(vs, &backend);
1214 case VHOST_SCSI_CLEAR_ENDPOINT:
1215 if (copy_from_user(&backend, argp, sizeof backend))
1216 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001217 if (backend.reserved != 0)
1218 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001219
1220 return vhost_scsi_clear_endpoint(vs, &backend);
1221 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001222 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001223 return -EFAULT;
1224 return 0;
1225 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001226 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001227 if (copy_to_user(featurep, &features, sizeof features))
1228 return -EFAULT;
1229 return 0;
1230 case VHOST_SET_FEATURES:
1231 if (copy_from_user(&features, featurep, sizeof features))
1232 return -EFAULT;
1233 return vhost_scsi_set_features(vs, features);
1234 default:
1235 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001236 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1237 /* TODO: flush backend after dev ioctl. */
1238 if (r == -ENOIOCTLCMD)
1239 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001240 mutex_unlock(&vs->dev.mutex);
1241 return r;
1242 }
1243}
1244
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001245#ifdef CONFIG_COMPAT
1246static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1247 unsigned long arg)
1248{
1249 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1250}
1251#endif
1252
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001253static const struct file_operations vhost_scsi_fops = {
1254 .owner = THIS_MODULE,
1255 .release = vhost_scsi_release,
1256 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001257#ifdef CONFIG_COMPAT
1258 .compat_ioctl = vhost_scsi_compat_ioctl,
1259#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001260 .open = vhost_scsi_open,
1261 .llseek = noop_llseek,
1262};
1263
1264static struct miscdevice vhost_scsi_misc = {
1265 MISC_DYNAMIC_MINOR,
1266 "vhost-scsi",
1267 &vhost_scsi_fops,
1268};
1269
1270static int __init vhost_scsi_register(void)
1271{
1272 return misc_register(&vhost_scsi_misc);
1273}
1274
1275static int vhost_scsi_deregister(void)
1276{
1277 return misc_deregister(&vhost_scsi_misc);
1278}
1279
1280static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1281{
1282 switch (tport->tport_proto_id) {
1283 case SCSI_PROTOCOL_SAS:
1284 return "SAS";
1285 case SCSI_PROTOCOL_FCP:
1286 return "FCP";
1287 case SCSI_PROTOCOL_ISCSI:
1288 return "iSCSI";
1289 default:
1290 break;
1291 }
1292
1293 return "Unknown";
1294}
1295
Asias Hea6c9af82013-04-25 15:35:21 +08001296static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1297 struct se_lun *lun, bool plug)
1298{
1299
1300 struct vhost_scsi *vs = tpg->vhost_scsi;
1301 struct vhost_virtqueue *vq;
1302 u32 reason;
1303
1304 if (!vs)
1305 return;
1306
1307 mutex_lock(&vs->dev.mutex);
1308 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1309 mutex_unlock(&vs->dev.mutex);
1310 return;
1311 }
1312
1313 if (plug)
1314 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1315 else
1316 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1317
1318 vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
1319 mutex_lock(&vq->mutex);
1320 tcm_vhost_send_evt(vs, tpg, lun,
1321 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1322 mutex_unlock(&vq->mutex);
1323 mutex_unlock(&vs->dev.mutex);
1324}
1325
1326static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1327{
1328 tcm_vhost_do_plug(tpg, lun, true);
1329}
1330
1331static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1332{
1333 tcm_vhost_do_plug(tpg, lun, false);
1334}
1335
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001336static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001337 struct se_lun *lun)
1338{
1339 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1340 struct tcm_vhost_tpg, se_tpg);
1341
Asias Hea6c9af82013-04-25 15:35:21 +08001342 mutex_lock(&tcm_vhost_mutex);
1343
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001344 mutex_lock(&tv_tpg->tv_tpg_mutex);
1345 tv_tpg->tv_tpg_port_count++;
1346 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001347
Asias Hea6c9af82013-04-25 15:35:21 +08001348 tcm_vhost_hotplug(tv_tpg, lun);
1349
1350 mutex_unlock(&tcm_vhost_mutex);
1351
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001352 return 0;
1353}
1354
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001355static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Asias Hea6c9af82013-04-25 15:35:21 +08001356 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001357{
1358 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1359 struct tcm_vhost_tpg, se_tpg);
1360
Asias Hea6c9af82013-04-25 15:35:21 +08001361 mutex_lock(&tcm_vhost_mutex);
1362
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001363 mutex_lock(&tv_tpg->tv_tpg_mutex);
1364 tv_tpg->tv_tpg_port_count--;
1365 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001366
1367 tcm_vhost_hotunplug(tv_tpg, lun);
1368
1369 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001370}
1371
1372static struct se_node_acl *tcm_vhost_make_nodeacl(
1373 struct se_portal_group *se_tpg,
1374 struct config_group *group,
1375 const char *name)
1376{
1377 struct se_node_acl *se_nacl, *se_nacl_new;
1378 struct tcm_vhost_nacl *nacl;
1379 u64 wwpn = 0;
1380 u32 nexus_depth;
1381
1382 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1383 return ERR_PTR(-EINVAL); */
1384 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1385 if (!se_nacl_new)
1386 return ERR_PTR(-ENOMEM);
1387
1388 nexus_depth = 1;
1389 /*
1390 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1391 * when converting a NodeACL from demo mode -> explict
1392 */
1393 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1394 name, nexus_depth);
1395 if (IS_ERR(se_nacl)) {
1396 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1397 return se_nacl;
1398 }
1399 /*
1400 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1401 */
1402 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1403 nacl->iport_wwpn = wwpn;
1404
1405 return se_nacl;
1406}
1407
1408static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1409{
1410 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1411 struct tcm_vhost_nacl, se_node_acl);
1412 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1413 kfree(nacl);
1414}
1415
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001416static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001417 const char *name)
1418{
1419 struct se_portal_group *se_tpg;
1420 struct tcm_vhost_nexus *tv_nexus;
1421
1422 mutex_lock(&tv_tpg->tv_tpg_mutex);
1423 if (tv_tpg->tpg_nexus) {
1424 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1425 pr_debug("tv_tpg->tpg_nexus already exists\n");
1426 return -EEXIST;
1427 }
1428 se_tpg = &tv_tpg->se_tpg;
1429
1430 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1431 if (!tv_nexus) {
1432 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1433 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1434 return -ENOMEM;
1435 }
1436 /*
1437 * Initialize the struct se_session pointer
1438 */
1439 tv_nexus->tvn_se_sess = transport_init_session();
1440 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1441 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1442 kfree(tv_nexus);
1443 return -ENOMEM;
1444 }
1445 /*
1446 * Since we are running in 'demo mode' this call with generate a
1447 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1448 * the SCSI Initiator port name of the passed configfs group 'name'.
1449 */
1450 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1451 se_tpg, (unsigned char *)name);
1452 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1453 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1454 pr_debug("core_tpg_check_initiator_node_acl() failed"
1455 " for %s\n", name);
1456 transport_free_session(tv_nexus->tvn_se_sess);
1457 kfree(tv_nexus);
1458 return -ENOMEM;
1459 }
1460 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001461 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001462 * call to __transport_register_session()
1463 */
1464 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1465 tv_nexus->tvn_se_sess, tv_nexus);
1466 tv_tpg->tpg_nexus = tv_nexus;
1467
1468 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1469 return 0;
1470}
1471
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001472static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001473{
1474 struct se_session *se_sess;
1475 struct tcm_vhost_nexus *tv_nexus;
1476
1477 mutex_lock(&tpg->tv_tpg_mutex);
1478 tv_nexus = tpg->tpg_nexus;
1479 if (!tv_nexus) {
1480 mutex_unlock(&tpg->tv_tpg_mutex);
1481 return -ENODEV;
1482 }
1483
1484 se_sess = tv_nexus->tvn_se_sess;
1485 if (!se_sess) {
1486 mutex_unlock(&tpg->tv_tpg_mutex);
1487 return -ENODEV;
1488 }
1489
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001490 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001491 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001492 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001493 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001494 tpg->tv_tpg_port_count);
1495 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001496 }
1497
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001498 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001499 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001500 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001501 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001502 tpg->tv_tpg_vhost_count);
1503 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001504 }
1505
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001506 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001507 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1508 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1509 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001510 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001511 */
1512 transport_deregister_session(tv_nexus->tvn_se_sess);
1513 tpg->tpg_nexus = NULL;
1514 mutex_unlock(&tpg->tv_tpg_mutex);
1515
1516 kfree(tv_nexus);
1517 return 0;
1518}
1519
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001520static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001521 char *page)
1522{
1523 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1524 struct tcm_vhost_tpg, se_tpg);
1525 struct tcm_vhost_nexus *tv_nexus;
1526 ssize_t ret;
1527
1528 mutex_lock(&tv_tpg->tv_tpg_mutex);
1529 tv_nexus = tv_tpg->tpg_nexus;
1530 if (!tv_nexus) {
1531 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1532 return -ENODEV;
1533 }
1534 ret = snprintf(page, PAGE_SIZE, "%s\n",
1535 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1536 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1537
1538 return ret;
1539}
1540
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001541static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001542 const char *page,
1543 size_t count)
1544{
1545 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1546 struct tcm_vhost_tpg, se_tpg);
1547 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1548 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1549 int ret;
1550 /*
1551 * Shutdown the active I_T nexus if 'NULL' is passed..
1552 */
1553 if (!strncmp(page, "NULL", 4)) {
1554 ret = tcm_vhost_drop_nexus(tv_tpg);
1555 return (!ret) ? count : ret;
1556 }
1557 /*
1558 * Otherwise make sure the passed virtual Initiator port WWN matches
1559 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1560 * tcm_vhost_make_nexus().
1561 */
1562 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1563 pr_err("Emulated NAA Sas Address: %s, exceeds"
1564 " max: %d\n", page, TCM_VHOST_NAMELEN);
1565 return -EINVAL;
1566 }
1567 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1568
1569 ptr = strstr(i_port, "naa.");
1570 if (ptr) {
1571 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1572 pr_err("Passed SAS Initiator Port %s does not"
1573 " match target port protoid: %s\n", i_port,
1574 tcm_vhost_dump_proto_id(tport_wwn));
1575 return -EINVAL;
1576 }
1577 port_ptr = &i_port[0];
1578 goto check_newline;
1579 }
1580 ptr = strstr(i_port, "fc.");
1581 if (ptr) {
1582 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1583 pr_err("Passed FCP Initiator Port %s does not"
1584 " match target port protoid: %s\n", i_port,
1585 tcm_vhost_dump_proto_id(tport_wwn));
1586 return -EINVAL;
1587 }
1588 port_ptr = &i_port[3]; /* Skip over "fc." */
1589 goto check_newline;
1590 }
1591 ptr = strstr(i_port, "iqn.");
1592 if (ptr) {
1593 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1594 pr_err("Passed iSCSI Initiator Port %s does not"
1595 " match target port protoid: %s\n", i_port,
1596 tcm_vhost_dump_proto_id(tport_wwn));
1597 return -EINVAL;
1598 }
1599 port_ptr = &i_port[0];
1600 goto check_newline;
1601 }
1602 pr_err("Unable to locate prefix for emulated Initiator Port:"
1603 " %s\n", i_port);
1604 return -EINVAL;
1605 /*
1606 * Clear any trailing newline for the NAA WWN
1607 */
1608check_newline:
1609 if (i_port[strlen(i_port)-1] == '\n')
1610 i_port[strlen(i_port)-1] = '\0';
1611
1612 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1613 if (ret < 0)
1614 return ret;
1615
1616 return count;
1617}
1618
1619TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1620
1621static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1622 &tcm_vhost_tpg_nexus.attr,
1623 NULL,
1624};
1625
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001626static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001627 struct config_group *group,
1628 const char *name)
1629{
1630 struct tcm_vhost_tport *tport = container_of(wwn,
1631 struct tcm_vhost_tport, tport_wwn);
1632
1633 struct tcm_vhost_tpg *tpg;
1634 unsigned long tpgt;
1635 int ret;
1636
1637 if (strstr(name, "tpgt_") != name)
1638 return ERR_PTR(-EINVAL);
1639 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1640 return ERR_PTR(-EINVAL);
1641
1642 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1643 if (!tpg) {
1644 pr_err("Unable to allocate struct tcm_vhost_tpg");
1645 return ERR_PTR(-ENOMEM);
1646 }
1647 mutex_init(&tpg->tv_tpg_mutex);
1648 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1649 tpg->tport = tport;
1650 tpg->tport_tpgt = tpgt;
1651
1652 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1653 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1654 if (ret < 0) {
1655 kfree(tpg);
1656 return NULL;
1657 }
1658 mutex_lock(&tcm_vhost_mutex);
1659 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1660 mutex_unlock(&tcm_vhost_mutex);
1661
1662 return &tpg->se_tpg;
1663}
1664
1665static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1666{
1667 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1668 struct tcm_vhost_tpg, se_tpg);
1669
1670 mutex_lock(&tcm_vhost_mutex);
1671 list_del(&tpg->tv_tpg_list);
1672 mutex_unlock(&tcm_vhost_mutex);
1673 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001674 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001675 */
1676 tcm_vhost_drop_nexus(tpg);
1677 /*
1678 * Deregister the se_tpg from TCM..
1679 */
1680 core_tpg_deregister(se_tpg);
1681 kfree(tpg);
1682}
1683
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001684static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001685 struct config_group *group,
1686 const char *name)
1687{
1688 struct tcm_vhost_tport *tport;
1689 char *ptr;
1690 u64 wwpn = 0;
1691 int off = 0;
1692
1693 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1694 return ERR_PTR(-EINVAL); */
1695
1696 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1697 if (!tport) {
1698 pr_err("Unable to allocate struct tcm_vhost_tport");
1699 return ERR_PTR(-ENOMEM);
1700 }
1701 tport->tport_wwpn = wwpn;
1702 /*
1703 * Determine the emulated Protocol Identifier and Target Port Name
1704 * based on the incoming configfs directory name.
1705 */
1706 ptr = strstr(name, "naa.");
1707 if (ptr) {
1708 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1709 goto check_len;
1710 }
1711 ptr = strstr(name, "fc.");
1712 if (ptr) {
1713 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1714 off = 3; /* Skip over "fc." */
1715 goto check_len;
1716 }
1717 ptr = strstr(name, "iqn.");
1718 if (ptr) {
1719 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1720 goto check_len;
1721 }
1722
1723 pr_err("Unable to locate prefix for emulated Target Port:"
1724 " %s\n", name);
1725 kfree(tport);
1726 return ERR_PTR(-EINVAL);
1727
1728check_len:
1729 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1730 pr_err("Emulated %s Address: %s, exceeds"
1731 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1732 TCM_VHOST_NAMELEN);
1733 kfree(tport);
1734 return ERR_PTR(-EINVAL);
1735 }
1736 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1737
1738 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1739 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1740
1741 return &tport->tport_wwn;
1742}
1743
1744static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1745{
1746 struct tcm_vhost_tport *tport = container_of(wwn,
1747 struct tcm_vhost_tport, tport_wwn);
1748
1749 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1750 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1751 tport->tport_name);
1752
1753 kfree(tport);
1754}
1755
1756static ssize_t tcm_vhost_wwn_show_attr_version(
1757 struct target_fabric_configfs *tf,
1758 char *page)
1759{
1760 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1761 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1762 utsname()->machine);
1763}
1764
1765TF_WWN_ATTR_RO(tcm_vhost, version);
1766
1767static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1768 &tcm_vhost_wwn_version.attr,
1769 NULL,
1770};
1771
1772static struct target_core_fabric_ops tcm_vhost_ops = {
1773 .get_fabric_name = tcm_vhost_get_fabric_name,
1774 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1775 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1776 .tpg_get_tag = tcm_vhost_get_tag,
1777 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1778 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1779 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1780 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1781 .tpg_check_demo_mode = tcm_vhost_check_true,
1782 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1783 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1784 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1785 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1786 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1787 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1788 .release_cmd = tcm_vhost_release_cmd,
1789 .shutdown_session = tcm_vhost_shutdown_session,
1790 .close_session = tcm_vhost_close_session,
1791 .sess_get_index = tcm_vhost_sess_get_index,
1792 .sess_get_initiator_sid = NULL,
1793 .write_pending = tcm_vhost_write_pending,
1794 .write_pending_status = tcm_vhost_write_pending_status,
1795 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1796 .get_task_tag = tcm_vhost_get_task_tag,
1797 .get_cmd_state = tcm_vhost_get_cmd_state,
1798 .queue_data_in = tcm_vhost_queue_data_in,
1799 .queue_status = tcm_vhost_queue_status,
1800 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001801 /*
1802 * Setup callers for generic logic in target_core_fabric_configfs.c
1803 */
1804 .fabric_make_wwn = tcm_vhost_make_tport,
1805 .fabric_drop_wwn = tcm_vhost_drop_tport,
1806 .fabric_make_tpg = tcm_vhost_make_tpg,
1807 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1808 .fabric_post_link = tcm_vhost_port_link,
1809 .fabric_pre_unlink = tcm_vhost_port_unlink,
1810 .fabric_make_np = NULL,
1811 .fabric_drop_np = NULL,
1812 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1813 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1814};
1815
1816static int tcm_vhost_register_configfs(void)
1817{
1818 struct target_fabric_configfs *fabric;
1819 int ret;
1820
1821 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1822 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1823 utsname()->machine);
1824 /*
1825 * Register the top level struct config_item_type with TCM core
1826 */
1827 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1828 if (IS_ERR(fabric)) {
1829 pr_err("target_fabric_configfs_init() failed\n");
1830 return PTR_ERR(fabric);
1831 }
1832 /*
1833 * Setup fabric->tf_ops from our local tcm_vhost_ops
1834 */
1835 fabric->tf_ops = tcm_vhost_ops;
1836 /*
1837 * Setup default attribute lists for various fabric->tf_cit_tmpl
1838 */
1839 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1840 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1841 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1842 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1843 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1844 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1845 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1846 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1847 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1848 /*
1849 * Register the fabric for use within TCM
1850 */
1851 ret = target_fabric_configfs_register(fabric);
1852 if (ret < 0) {
1853 pr_err("target_fabric_configfs_register() failed"
1854 " for TCM_VHOST\n");
1855 return ret;
1856 }
1857 /*
1858 * Setup our local pointer to *fabric
1859 */
1860 tcm_vhost_fabric_configfs = fabric;
1861 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1862 return 0;
1863};
1864
1865static void tcm_vhost_deregister_configfs(void)
1866{
1867 if (!tcm_vhost_fabric_configfs)
1868 return;
1869
1870 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1871 tcm_vhost_fabric_configfs = NULL;
1872 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1873};
1874
1875static int __init tcm_vhost_init(void)
1876{
1877 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001878 /*
1879 * Use our own dedicated workqueue for submitting I/O into
1880 * target core to avoid contention within system_wq.
1881 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001882 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1883 if (!tcm_vhost_workqueue)
1884 goto out;
1885
1886 ret = vhost_scsi_register();
1887 if (ret < 0)
1888 goto out_destroy_workqueue;
1889
1890 ret = tcm_vhost_register_configfs();
1891 if (ret < 0)
1892 goto out_vhost_scsi_deregister;
1893
1894 return 0;
1895
1896out_vhost_scsi_deregister:
1897 vhost_scsi_deregister();
1898out_destroy_workqueue:
1899 destroy_workqueue(tcm_vhost_workqueue);
1900out:
1901 return ret;
1902};
1903
1904static void tcm_vhost_exit(void)
1905{
1906 tcm_vhost_deregister_configfs();
1907 vhost_scsi_deregister();
1908 destroy_workqueue(tcm_vhost_workqueue);
1909};
1910
1911MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1912MODULE_LICENSE("GPL");
1913module_init(tcm_vhost_init);
1914module_exit(tcm_vhost_exit);