Commit | Line | Data |
---|---|---|
057cbf49 NB |
1 | /******************************************************************************* |
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> | |
37 | #include <linux/vhost.h> | |
38 | #include <linux/fs.h> | |
39 | #include <linux/miscdevice.h> | |
40 | #include <asm/unaligned.h> | |
41 | #include <scsi/scsi.h> | |
42 | #include <scsi/scsi_tcq.h> | |
43 | #include <target/target_core_base.h> | |
44 | #include <target/target_core_fabric.h> | |
45 | #include <target/target_core_fabric_configfs.h> | |
46 | #include <target/target_core_configfs.h> | |
47 | #include <target/configfs_macros.h> | |
48 | #include <linux/vhost.h> | |
49 | #include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */ | |
50 | #include <linux/virtio_scsi.h> | |
51 | ||
52 | #include "vhost.c" | |
53 | #include "vhost.h" | |
54 | #include "tcm_vhost.h" | |
55 | ||
101998f6 NB |
56 | enum { |
57 | VHOST_SCSI_VQ_CTL = 0, | |
58 | VHOST_SCSI_VQ_EVT = 1, | |
59 | VHOST_SCSI_VQ_IO = 2, | |
60 | }; | |
61 | ||
057cbf49 | 62 | struct vhost_scsi { |
101998f6 | 63 | struct tcm_vhost_tpg *vs_tpg; /* Protected by vhost_scsi->dev.mutex */ |
057cbf49 NB |
64 | struct vhost_dev dev; |
65 | struct vhost_virtqueue vqs[3]; | |
66 | ||
67 | struct vhost_work vs_completion_work; /* cmd completion work item */ | |
68 | struct list_head vs_completion_list; /* cmd completion queue */ | |
69 | spinlock_t vs_completion_lock; /* protects s_completion_list */ | |
70 | }; | |
71 | ||
72 | /* Local pointer to allocated TCM configfs fabric module */ | |
73 | static struct target_fabric_configfs *tcm_vhost_fabric_configfs; | |
74 | ||
75 | static struct workqueue_struct *tcm_vhost_workqueue; | |
76 | ||
77 | /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */ | |
78 | static DEFINE_MUTEX(tcm_vhost_mutex); | |
79 | static LIST_HEAD(tcm_vhost_list); | |
80 | ||
81 | static int tcm_vhost_check_true(struct se_portal_group *se_tpg) | |
82 | { | |
83 | return 1; | |
84 | } | |
85 | ||
86 | static int tcm_vhost_check_false(struct se_portal_group *se_tpg) | |
87 | { | |
88 | return 0; | |
89 | } | |
90 | ||
91 | static char *tcm_vhost_get_fabric_name(void) | |
92 | { | |
93 | return "vhost"; | |
94 | } | |
95 | ||
96 | static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg) | |
97 | { | |
98 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
99 | struct tcm_vhost_tpg, se_tpg); | |
100 | struct tcm_vhost_tport *tport = tpg->tport; | |
101 | ||
102 | switch (tport->tport_proto_id) { | |
103 | case SCSI_PROTOCOL_SAS: | |
104 | return sas_get_fabric_proto_ident(se_tpg); | |
105 | case SCSI_PROTOCOL_FCP: | |
106 | return fc_get_fabric_proto_ident(se_tpg); | |
107 | case SCSI_PROTOCOL_ISCSI: | |
108 | return iscsi_get_fabric_proto_ident(se_tpg); | |
109 | default: | |
110 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
111 | " SAS emulation\n", tport->tport_proto_id); | |
112 | break; | |
113 | } | |
114 | ||
115 | return sas_get_fabric_proto_ident(se_tpg); | |
116 | } | |
117 | ||
118 | static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg) | |
119 | { | |
120 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
121 | struct tcm_vhost_tpg, se_tpg); | |
122 | struct tcm_vhost_tport *tport = tpg->tport; | |
123 | ||
124 | return &tport->tport_name[0]; | |
125 | } | |
126 | ||
127 | static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg) | |
128 | { | |
129 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
130 | struct tcm_vhost_tpg, se_tpg); | |
131 | return tpg->tport_tpgt; | |
132 | } | |
133 | ||
134 | static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg) | |
135 | { | |
136 | return 1; | |
137 | } | |
138 | ||
101998f6 | 139 | static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg, |
057cbf49 NB |
140 | struct se_node_acl *se_nacl, |
141 | struct t10_pr_registration *pr_reg, | |
142 | int *format_code, | |
143 | unsigned char *buf) | |
144 | { | |
145 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
146 | struct tcm_vhost_tpg, se_tpg); | |
147 | struct tcm_vhost_tport *tport = tpg->tport; | |
148 | ||
149 | switch (tport->tport_proto_id) { | |
150 | case SCSI_PROTOCOL_SAS: | |
151 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
152 | format_code, buf); | |
153 | case SCSI_PROTOCOL_FCP: | |
154 | return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
155 | format_code, buf); | |
156 | case SCSI_PROTOCOL_ISCSI: | |
157 | return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
158 | format_code, buf); | |
159 | default: | |
160 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
161 | " SAS emulation\n", tport->tport_proto_id); | |
162 | break; | |
163 | } | |
164 | ||
165 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
166 | format_code, buf); | |
167 | } | |
168 | ||
101998f6 | 169 | static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg, |
057cbf49 NB |
170 | struct se_node_acl *se_nacl, |
171 | struct t10_pr_registration *pr_reg, | |
172 | int *format_code) | |
173 | { | |
174 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
175 | struct tcm_vhost_tpg, se_tpg); | |
176 | struct tcm_vhost_tport *tport = tpg->tport; | |
177 | ||
178 | switch (tport->tport_proto_id) { | |
179 | case SCSI_PROTOCOL_SAS: | |
180 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
181 | format_code); | |
182 | case SCSI_PROTOCOL_FCP: | |
183 | return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
184 | format_code); | |
185 | case SCSI_PROTOCOL_ISCSI: | |
186 | return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
187 | format_code); | |
188 | default: | |
189 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
190 | " SAS emulation\n", tport->tport_proto_id); | |
191 | break; | |
192 | } | |
193 | ||
194 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
195 | format_code); | |
196 | } | |
197 | ||
101998f6 | 198 | static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg, |
057cbf49 NB |
199 | const char *buf, |
200 | u32 *out_tid_len, | |
201 | char **port_nexus_ptr) | |
202 | { | |
203 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
204 | struct tcm_vhost_tpg, se_tpg); | |
205 | struct tcm_vhost_tport *tport = tpg->tport; | |
206 | ||
207 | switch (tport->tport_proto_id) { | |
208 | case SCSI_PROTOCOL_SAS: | |
209 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
210 | port_nexus_ptr); | |
211 | case SCSI_PROTOCOL_FCP: | |
212 | return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
213 | port_nexus_ptr); | |
214 | case SCSI_PROTOCOL_ISCSI: | |
215 | return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
216 | port_nexus_ptr); | |
217 | default: | |
218 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
219 | " SAS emulation\n", tport->tport_proto_id); | |
220 | break; | |
221 | } | |
222 | ||
223 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
224 | port_nexus_ptr); | |
225 | } | |
226 | ||
227 | static struct se_node_acl *tcm_vhost_alloc_fabric_acl( | |
228 | struct se_portal_group *se_tpg) | |
229 | { | |
230 | struct tcm_vhost_nacl *nacl; | |
231 | ||
232 | nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL); | |
233 | if (!nacl) { | |
234 | pr_err("Unable to alocate struct tcm_vhost_nacl\n"); | |
235 | return NULL; | |
236 | } | |
237 | ||
238 | return &nacl->se_node_acl; | |
239 | } | |
240 | ||
101998f6 | 241 | static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg, |
057cbf49 NB |
242 | struct se_node_acl *se_nacl) |
243 | { | |
244 | struct tcm_vhost_nacl *nacl = container_of(se_nacl, | |
245 | struct tcm_vhost_nacl, se_node_acl); | |
246 | kfree(nacl); | |
247 | } | |
248 | ||
249 | static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg) | |
250 | { | |
251 | return 1; | |
252 | } | |
253 | ||
254 | static void tcm_vhost_release_cmd(struct se_cmd *se_cmd) | |
255 | { | |
256 | return; | |
257 | } | |
258 | ||
259 | static int tcm_vhost_shutdown_session(struct se_session *se_sess) | |
260 | { | |
261 | return 0; | |
262 | } | |
263 | ||
264 | static void tcm_vhost_close_session(struct se_session *se_sess) | |
265 | { | |
266 | return; | |
267 | } | |
268 | ||
269 | static u32 tcm_vhost_sess_get_index(struct se_session *se_sess) | |
270 | { | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static int tcm_vhost_write_pending(struct se_cmd *se_cmd) | |
275 | { | |
276 | /* Go ahead and process the write immediately */ | |
277 | target_execute_cmd(se_cmd); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd) | |
282 | { | |
283 | return 0; | |
284 | } | |
285 | ||
286 | static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl) | |
287 | { | |
288 | return; | |
289 | } | |
290 | ||
291 | static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd) | |
292 | { | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd) | |
297 | { | |
298 | return 0; | |
299 | } | |
300 | ||
101998f6 NB |
301 | static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd) |
302 | { | |
303 | struct vhost_scsi *vs = tv_cmd->tvc_vhost; | |
304 | ||
305 | spin_lock_bh(&vs->vs_completion_lock); | |
306 | list_add_tail(&tv_cmd->tvc_completion_list, &vs->vs_completion_list); | |
307 | spin_unlock_bh(&vs->vs_completion_lock); | |
308 | ||
309 | vhost_work_queue(&vs->dev, &vs->vs_completion_work); | |
310 | } | |
057cbf49 NB |
311 | |
312 | static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd) | |
313 | { | |
314 | struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, | |
315 | struct tcm_vhost_cmd, tvc_se_cmd); | |
316 | vhost_scsi_complete_cmd(tv_cmd); | |
317 | return 0; | |
318 | } | |
319 | ||
320 | static int tcm_vhost_queue_status(struct se_cmd *se_cmd) | |
321 | { | |
322 | struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, | |
323 | struct tcm_vhost_cmd, tvc_se_cmd); | |
324 | vhost_scsi_complete_cmd(tv_cmd); | |
325 | return 0; | |
326 | } | |
327 | ||
328 | static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd) | |
329 | { | |
330 | return 0; | |
331 | } | |
332 | ||
057cbf49 NB |
333 | static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd) |
334 | { | |
335 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; | |
336 | ||
337 | /* TODO locking against target/backend threads? */ | |
338 | transport_generic_free_cmd(se_cmd, 1); | |
339 | ||
340 | if (tv_cmd->tvc_sgl_count) { | |
341 | u32 i; | |
342 | for (i = 0; i < tv_cmd->tvc_sgl_count; i++) | |
343 | put_page(sg_page(&tv_cmd->tvc_sgl[i])); | |
344 | ||
345 | kfree(tv_cmd->tvc_sgl); | |
346 | } | |
347 | ||
348 | kfree(tv_cmd); | |
349 | } | |
350 | ||
351 | /* Dequeue a command from the completion list */ | |
352 | static struct tcm_vhost_cmd *vhost_scsi_get_cmd_from_completion( | |
353 | struct vhost_scsi *vs) | |
354 | { | |
355 | struct tcm_vhost_cmd *tv_cmd = NULL; | |
356 | ||
357 | spin_lock_bh(&vs->vs_completion_lock); | |
358 | if (list_empty(&vs->vs_completion_list)) { | |
359 | spin_unlock_bh(&vs->vs_completion_lock); | |
360 | return NULL; | |
361 | } | |
362 | ||
363 | list_for_each_entry(tv_cmd, &vs->vs_completion_list, | |
364 | tvc_completion_list) { | |
365 | list_del(&tv_cmd->tvc_completion_list); | |
366 | break; | |
367 | } | |
368 | spin_unlock_bh(&vs->vs_completion_lock); | |
369 | return tv_cmd; | |
370 | } | |
371 | ||
372 | /* Fill in status and signal that we are done processing this command | |
373 | * | |
374 | * This is scheduled in the vhost work queue so we are called with the owner | |
375 | * process mm and can access the vring. | |
376 | */ | |
377 | static void vhost_scsi_complete_cmd_work(struct vhost_work *work) | |
378 | { | |
379 | struct vhost_scsi *vs = container_of(work, struct vhost_scsi, | |
380 | vs_completion_work); | |
381 | struct tcm_vhost_cmd *tv_cmd; | |
382 | ||
101998f6 | 383 | while ((tv_cmd = vhost_scsi_get_cmd_from_completion(vs))) { |
057cbf49 NB |
384 | struct virtio_scsi_cmd_resp v_rsp; |
385 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; | |
386 | int ret; | |
387 | ||
388 | pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__, | |
389 | tv_cmd, se_cmd->residual_count, se_cmd->scsi_status); | |
390 | ||
391 | memset(&v_rsp, 0, sizeof(v_rsp)); | |
392 | v_rsp.resid = se_cmd->residual_count; | |
393 | /* TODO is status_qualifier field needed? */ | |
394 | v_rsp.status = se_cmd->scsi_status; | |
395 | v_rsp.sense_len = se_cmd->scsi_sense_length; | |
396 | memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf, | |
397 | v_rsp.sense_len); | |
398 | ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp)); | |
399 | if (likely(ret == 0)) | |
400 | vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0); | |
401 | else | |
402 | pr_err("Faulted on virtio_scsi_cmd_resp\n"); | |
403 | ||
404 | vhost_scsi_free_cmd(tv_cmd); | |
405 | } | |
406 | ||
407 | vhost_signal(&vs->dev, &vs->vqs[2]); | |
408 | } | |
409 | ||
057cbf49 NB |
410 | static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd( |
411 | struct tcm_vhost_tpg *tv_tpg, | |
412 | struct virtio_scsi_cmd_req *v_req, | |
413 | u32 exp_data_len, | |
414 | int data_direction) | |
415 | { | |
416 | struct tcm_vhost_cmd *tv_cmd; | |
417 | struct tcm_vhost_nexus *tv_nexus; | |
418 | struct se_portal_group *se_tpg = &tv_tpg->se_tpg; | |
419 | struct se_session *se_sess; | |
420 | struct se_cmd *se_cmd; | |
421 | int sam_task_attr; | |
422 | ||
423 | tv_nexus = tv_tpg->tpg_nexus; | |
424 | if (!tv_nexus) { | |
425 | pr_err("Unable to locate active struct tcm_vhost_nexus\n"); | |
426 | return ERR_PTR(-EIO); | |
427 | } | |
428 | se_sess = tv_nexus->tvn_se_sess; | |
429 | ||
430 | tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC); | |
431 | if (!tv_cmd) { | |
432 | pr_err("Unable to allocate struct tcm_vhost_cmd\n"); | |
433 | return ERR_PTR(-ENOMEM); | |
434 | } | |
435 | INIT_LIST_HEAD(&tv_cmd->tvc_completion_list); | |
436 | tv_cmd->tvc_tag = v_req->tag; | |
437 | ||
438 | se_cmd = &tv_cmd->tvc_se_cmd; | |
439 | /* | |
440 | * Locate the SAM Task Attr from virtio_scsi_cmd_req | |
441 | */ | |
442 | sam_task_attr = v_req->task_attr; | |
443 | /* | |
444 | * Initialize struct se_cmd descriptor from TCM infrastructure | |
445 | */ | |
446 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, exp_data_len, | |
447 | data_direction, sam_task_attr, | |
448 | &tv_cmd->tvc_sense_buf[0]); | |
449 | ||
450 | #if 0 /* FIXME: vhost_scsi_allocate_cmd() BIDI operation */ | |
451 | if (bidi) | |
452 | se_cmd->se_cmd_flags |= SCF_BIDI; | |
453 | #endif | |
454 | return tv_cmd; | |
455 | } | |
456 | ||
457 | /* | |
458 | * Map a user memory range into a scatterlist | |
459 | * | |
460 | * Returns the number of scatterlist entries used or -errno on error. | |
461 | */ | |
462 | static int vhost_scsi_map_to_sgl(struct scatterlist *sgl, | |
463 | unsigned int sgl_count, void __user *ptr, size_t len, int write) | |
464 | { | |
465 | struct scatterlist *sg = sgl; | |
466 | unsigned int npages = 0; | |
467 | int ret; | |
468 | ||
469 | while (len > 0) { | |
470 | struct page *page; | |
471 | unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK; | |
472 | unsigned int nbytes = min_t(unsigned int, | |
473 | PAGE_SIZE - offset, len); | |
474 | ||
475 | if (npages == sgl_count) { | |
476 | ret = -ENOBUFS; | |
477 | goto err; | |
478 | } | |
479 | ||
480 | ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page); | |
481 | BUG_ON(ret == 0); /* we should either get our page or fail */ | |
482 | if (ret < 0) | |
483 | goto err; | |
484 | ||
485 | sg_set_page(sg, page, nbytes, offset); | |
486 | ptr += nbytes; | |
487 | len -= nbytes; | |
488 | sg++; | |
489 | npages++; | |
490 | } | |
491 | return npages; | |
492 | ||
493 | err: | |
494 | /* Put pages that we hold */ | |
495 | for (sg = sgl; sg != &sgl[npages]; sg++) | |
496 | put_page(sg_page(sg)); | |
497 | return ret; | |
498 | } | |
499 | ||
500 | static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd, | |
501 | struct iovec *iov, unsigned int niov, int write) | |
502 | { | |
503 | int ret; | |
504 | unsigned int i; | |
505 | u32 sgl_count; | |
506 | struct scatterlist *sg; | |
507 | ||
508 | /* | |
509 | * Find out how long sglist needs to be | |
510 | */ | |
511 | sgl_count = 0; | |
512 | for (i = 0; i < niov; i++) { | |
513 | sgl_count += (((uintptr_t)iov[i].iov_base + iov[i].iov_len + | |
514 | PAGE_SIZE - 1) >> PAGE_SHIFT) - | |
515 | ((uintptr_t)iov[i].iov_base >> PAGE_SHIFT); | |
516 | } | |
517 | /* TODO overflow checking */ | |
518 | ||
519 | sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC); | |
520 | if (!sg) | |
521 | return -ENOMEM; | |
f0e0e9bb FW |
522 | pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__, |
523 | sg, sgl_count, !sg); | |
057cbf49 NB |
524 | sg_init_table(sg, sgl_count); |
525 | ||
526 | tv_cmd->tvc_sgl = sg; | |
527 | tv_cmd->tvc_sgl_count = sgl_count; | |
528 | ||
529 | pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count); | |
530 | for (i = 0; i < niov; i++) { | |
531 | ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base, | |
532 | iov[i].iov_len, write); | |
533 | if (ret < 0) { | |
534 | for (i = 0; i < tv_cmd->tvc_sgl_count; i++) | |
535 | put_page(sg_page(&tv_cmd->tvc_sgl[i])); | |
536 | kfree(tv_cmd->tvc_sgl); | |
537 | tv_cmd->tvc_sgl = NULL; | |
538 | tv_cmd->tvc_sgl_count = 0; | |
539 | return ret; | |
540 | } | |
541 | ||
542 | sg += ret; | |
543 | sgl_count -= ret; | |
544 | } | |
545 | return 0; | |
546 | } | |
547 | ||
548 | static void tcm_vhost_submission_work(struct work_struct *work) | |
549 | { | |
550 | struct tcm_vhost_cmd *tv_cmd = | |
551 | container_of(work, struct tcm_vhost_cmd, work); | |
552 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; | |
553 | struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL; | |
554 | int rc, sg_no_bidi = 0; | |
555 | /* | |
556 | * Locate the struct se_lun pointer based on v_req->lun, and | |
557 | * attach it to struct se_cmd | |
558 | */ | |
559 | rc = transport_lookup_cmd_lun(&tv_cmd->tvc_se_cmd, tv_cmd->tvc_lun); | |
560 | if (rc < 0) { | |
561 | pr_err("Failed to look up lun: %d\n", tv_cmd->tvc_lun); | |
562 | transport_send_check_condition_and_sense(&tv_cmd->tvc_se_cmd, | |
563 | tv_cmd->tvc_se_cmd.scsi_sense_reason, 0); | |
564 | transport_generic_free_cmd(se_cmd, 0); | |
565 | return; | |
566 | } | |
567 | ||
568 | rc = target_setup_cmd_from_cdb(se_cmd, tv_cmd->tvc_cdb); | |
569 | if (rc == -ENOMEM) { | |
570 | transport_send_check_condition_and_sense(se_cmd, | |
571 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); | |
572 | transport_generic_free_cmd(se_cmd, 0); | |
573 | return; | |
574 | } else if (rc < 0) { | |
575 | if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT) | |
576 | tcm_vhost_queue_status(se_cmd); | |
577 | else | |
578 | transport_send_check_condition_and_sense(se_cmd, | |
579 | se_cmd->scsi_sense_reason, 0); | |
580 | transport_generic_free_cmd(se_cmd, 0); | |
581 | return; | |
582 | } | |
583 | ||
584 | if (tv_cmd->tvc_sgl_count) { | |
585 | sg_ptr = tv_cmd->tvc_sgl; | |
586 | /* | |
587 | * For BIDI commands, pass in the extra READ buffer | |
588 | * to transport_generic_map_mem_to_cmd() below.. | |
589 | */ | |
590 | /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */ | |
591 | #if 0 | |
592 | if (se_cmd->se_cmd_flags & SCF_BIDI) { | |
593 | sg_bidi_ptr = NULL; | |
594 | sg_no_bidi = 0; | |
595 | } | |
596 | #endif | |
597 | } else { | |
598 | sg_ptr = NULL; | |
599 | } | |
600 | ||
601 | rc = transport_generic_map_mem_to_cmd(se_cmd, sg_ptr, | |
602 | tv_cmd->tvc_sgl_count, sg_bidi_ptr, | |
603 | sg_no_bidi); | |
604 | if (rc < 0) { | |
605 | transport_send_check_condition_and_sense(se_cmd, | |
606 | se_cmd->scsi_sense_reason, 0); | |
607 | transport_generic_free_cmd(se_cmd, 0); | |
608 | return; | |
609 | } | |
610 | transport_handle_cdb_direct(se_cmd); | |
611 | } | |
612 | ||
613 | static void vhost_scsi_handle_vq(struct vhost_scsi *vs) | |
614 | { | |
615 | struct vhost_virtqueue *vq = &vs->vqs[2]; | |
616 | struct virtio_scsi_cmd_req v_req; | |
617 | struct tcm_vhost_tpg *tv_tpg; | |
618 | struct tcm_vhost_cmd *tv_cmd; | |
619 | u32 exp_data_len, data_first, data_num, data_direction; | |
620 | unsigned out, in, i; | |
621 | int head, ret; | |
622 | ||
623 | /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */ | |
624 | tv_tpg = vs->vs_tpg; | |
625 | if (unlikely(!tv_tpg)) { | |
626 | pr_err("%s endpoint not set\n", __func__); | |
627 | return; | |
628 | } | |
629 | ||
630 | mutex_lock(&vq->mutex); | |
631 | vhost_disable_notify(&vs->dev, vq); | |
632 | ||
633 | for (;;) { | |
634 | head = vhost_get_vq_desc(&vs->dev, vq, vq->iov, | |
635 | ARRAY_SIZE(vq->iov), &out, &in, | |
636 | NULL, NULL); | |
637 | pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", | |
638 | head, out, in); | |
639 | /* On error, stop handling until the next kick. */ | |
640 | if (unlikely(head < 0)) | |
641 | break; | |
642 | /* Nothing new? Wait for eventfd to tell us they refilled. */ | |
643 | if (head == vq->num) { | |
644 | if (unlikely(vhost_enable_notify(&vs->dev, vq))) { | |
645 | vhost_disable_notify(&vs->dev, vq); | |
646 | continue; | |
647 | } | |
648 | break; | |
649 | } | |
650 | ||
651 | /* FIXME: BIDI operation */ | |
652 | if (out == 1 && in == 1) { | |
653 | data_direction = DMA_NONE; | |
654 | data_first = 0; | |
655 | data_num = 0; | |
656 | } else if (out == 1 && in > 1) { | |
657 | data_direction = DMA_FROM_DEVICE; | |
658 | data_first = out + 1; | |
659 | data_num = in - 1; | |
660 | } else if (out > 1 && in == 1) { | |
661 | data_direction = DMA_TO_DEVICE; | |
662 | data_first = 1; | |
663 | data_num = out - 1; | |
664 | } else { | |
665 | vq_err(vq, "Invalid buffer layout out: %u in: %u\n", | |
666 | out, in); | |
667 | break; | |
668 | } | |
669 | ||
670 | /* | |
671 | * Check for a sane resp buffer so we can report errors to | |
672 | * the guest. | |
673 | */ | |
674 | if (unlikely(vq->iov[out].iov_len != | |
675 | sizeof(struct virtio_scsi_cmd_resp))) { | |
676 | vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu" | |
677 | " bytes\n", vq->iov[out].iov_len); | |
678 | break; | |
679 | } | |
680 | ||
681 | if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) { | |
682 | vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu" | |
683 | " bytes\n", vq->iov[0].iov_len); | |
684 | break; | |
685 | } | |
686 | pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p," | |
687 | " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req)); | |
688 | ret = __copy_from_user(&v_req, vq->iov[0].iov_base, | |
689 | sizeof(v_req)); | |
690 | if (unlikely(ret)) { | |
691 | vq_err(vq, "Faulted on virtio_scsi_cmd_req\n"); | |
692 | break; | |
693 | } | |
694 | ||
695 | exp_data_len = 0; | |
696 | for (i = 0; i < data_num; i++) | |
697 | exp_data_len += vq->iov[data_first + i].iov_len; | |
698 | ||
699 | tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req, | |
700 | exp_data_len, data_direction); | |
701 | if (IS_ERR(tv_cmd)) { | |
702 | vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n", | |
703 | PTR_ERR(tv_cmd)); | |
704 | break; | |
705 | } | |
706 | pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction" | |
707 | ": %d\n", tv_cmd, exp_data_len, data_direction); | |
708 | ||
709 | tv_cmd->tvc_vhost = vs; | |
710 | ||
711 | if (unlikely(vq->iov[out].iov_len != | |
712 | sizeof(struct virtio_scsi_cmd_resp))) { | |
713 | vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu" | |
714 | " bytes, out: %d, in: %d\n", | |
715 | vq->iov[out].iov_len, out, in); | |
716 | break; | |
717 | } | |
718 | ||
719 | tv_cmd->tvc_resp = vq->iov[out].iov_base; | |
720 | ||
721 | /* | |
722 | * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb | |
723 | * that will be used by tcm_vhost_new_cmd_map() and down into | |
724 | * target_setup_cmd_from_cdb() | |
725 | */ | |
726 | memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE); | |
727 | /* | |
728 | * Check that the recieved CDB size does not exceeded our | |
729 | * hardcoded max for tcm_vhost | |
730 | */ | |
731 | /* TODO what if cdb was too small for varlen cdb header? */ | |
732 | if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) > | |
733 | TCM_VHOST_MAX_CDB_SIZE)) { | |
734 | vq_err(vq, "Received SCSI CDB with command_size: %d that" | |
735 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", | |
736 | scsi_command_size(tv_cmd->tvc_cdb), | |
737 | TCM_VHOST_MAX_CDB_SIZE); | |
738 | break; /* TODO */ | |
739 | } | |
740 | tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF; | |
741 | ||
742 | pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", | |
743 | tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun); | |
744 | ||
745 | if (data_direction != DMA_NONE) { | |
746 | ret = vhost_scsi_map_iov_to_sgl(tv_cmd, | |
747 | &vq->iov[data_first], data_num, | |
748 | data_direction == DMA_TO_DEVICE); | |
749 | if (unlikely(ret)) { | |
750 | vq_err(vq, "Failed to map iov to sgl\n"); | |
751 | break; /* TODO */ | |
752 | } | |
753 | } | |
754 | ||
755 | /* | |
756 | * Save the descriptor from vhost_get_vq_desc() to be used to | |
757 | * complete the virtio-scsi request in TCM callback context via | |
758 | * tcm_vhost_queue_data_in() and tcm_vhost_queue_status() | |
759 | */ | |
760 | tv_cmd->tvc_vq_desc = head; | |
761 | /* | |
762 | * Dispatch tv_cmd descriptor for cmwq execution in process | |
763 | * context provided by tcm_vhost_workqueue. This also ensures | |
764 | * tv_cmd is executed on the same kworker CPU as this vhost | |
765 | * thread to gain positive L2 cache locality effects.. | |
766 | */ | |
767 | INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work); | |
768 | queue_work(tcm_vhost_workqueue, &tv_cmd->work); | |
769 | } | |
770 | ||
771 | mutex_unlock(&vq->mutex); | |
772 | } | |
773 | ||
774 | static void vhost_scsi_ctl_handle_kick(struct vhost_work *work) | |
775 | { | |
101998f6 | 776 | pr_debug("%s: The handling func for control queue.\n", __func__); |
057cbf49 NB |
777 | } |
778 | ||
779 | static void vhost_scsi_evt_handle_kick(struct vhost_work *work) | |
780 | { | |
101998f6 | 781 | pr_debug("%s: The handling func for event queue.\n", __func__); |
057cbf49 NB |
782 | } |
783 | ||
784 | static void vhost_scsi_handle_kick(struct vhost_work *work) | |
785 | { | |
786 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, | |
787 | poll.work); | |
788 | struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); | |
789 | ||
790 | vhost_scsi_handle_vq(vs); | |
791 | } | |
792 | ||
793 | /* | |
794 | * Called from vhost_scsi_ioctl() context to walk the list of available | |
795 | * tcm_vhost_tpg with an active struct tcm_vhost_nexus | |
796 | */ | |
797 | static int vhost_scsi_set_endpoint( | |
798 | struct vhost_scsi *vs, | |
799 | struct vhost_scsi_target *t) | |
800 | { | |
801 | struct tcm_vhost_tport *tv_tport; | |
802 | struct tcm_vhost_tpg *tv_tpg; | |
803 | int index; | |
804 | ||
805 | mutex_lock(&vs->dev.mutex); | |
806 | /* Verify that ring has been setup correctly. */ | |
807 | for (index = 0; index < vs->dev.nvqs; ++index) { | |
808 | /* Verify that ring has been setup correctly. */ | |
809 | if (!vhost_vq_access_ok(&vs->vqs[index])) { | |
810 | mutex_unlock(&vs->dev.mutex); | |
811 | return -EFAULT; | |
812 | } | |
813 | } | |
057cbf49 NB |
814 | mutex_unlock(&vs->dev.mutex); |
815 | ||
816 | mutex_lock(&tcm_vhost_mutex); | |
817 | list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) { | |
818 | mutex_lock(&tv_tpg->tv_tpg_mutex); | |
819 | if (!tv_tpg->tpg_nexus) { | |
820 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
821 | continue; | |
822 | } | |
101998f6 | 823 | if (tv_tpg->tv_tpg_vhost_count != 0) { |
057cbf49 NB |
824 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
825 | continue; | |
826 | } | |
827 | tv_tport = tv_tpg->tport; | |
828 | ||
829 | if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) && | |
830 | (tv_tpg->tport_tpgt == t->vhost_tpgt)) { | |
101998f6 | 831 | tv_tpg->tv_tpg_vhost_count++; |
057cbf49 NB |
832 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
833 | mutex_unlock(&tcm_vhost_mutex); | |
834 | ||
835 | mutex_lock(&vs->dev.mutex); | |
101998f6 NB |
836 | if (vs->vs_tpg) { |
837 | mutex_unlock(&vs->dev.mutex); | |
838 | mutex_lock(&tv_tpg->tv_tpg_mutex); | |
839 | tv_tpg->tv_tpg_vhost_count--; | |
840 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
841 | return -EEXIST; | |
842 | } | |
843 | ||
057cbf49 | 844 | vs->vs_tpg = tv_tpg; |
057cbf49 NB |
845 | smp_mb__after_atomic_inc(); |
846 | mutex_unlock(&vs->dev.mutex); | |
847 | return 0; | |
848 | } | |
849 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
850 | } | |
851 | mutex_unlock(&tcm_vhost_mutex); | |
852 | return -EINVAL; | |
853 | } | |
854 | ||
855 | static int vhost_scsi_clear_endpoint( | |
856 | struct vhost_scsi *vs, | |
857 | struct vhost_scsi_target *t) | |
858 | { | |
859 | struct tcm_vhost_tport *tv_tport; | |
860 | struct tcm_vhost_tpg *tv_tpg; | |
101998f6 | 861 | int index, ret; |
057cbf49 NB |
862 | |
863 | mutex_lock(&vs->dev.mutex); | |
864 | /* Verify that ring has been setup correctly. */ | |
865 | for (index = 0; index < vs->dev.nvqs; ++index) { | |
866 | if (!vhost_vq_access_ok(&vs->vqs[index])) { | |
101998f6 NB |
867 | ret = -EFAULT; |
868 | goto err; | |
057cbf49 NB |
869 | } |
870 | } | |
871 | ||
872 | if (!vs->vs_tpg) { | |
101998f6 NB |
873 | ret = -ENODEV; |
874 | goto err; | |
057cbf49 NB |
875 | } |
876 | tv_tpg = vs->vs_tpg; | |
877 | tv_tport = tv_tpg->tport; | |
878 | ||
879 | if (strcmp(tv_tport->tport_name, t->vhost_wwpn) || | |
880 | (tv_tpg->tport_tpgt != t->vhost_tpgt)) { | |
057cbf49 NB |
881 | pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu" |
882 | " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n", | |
883 | tv_tport->tport_name, tv_tpg->tport_tpgt, | |
884 | t->vhost_wwpn, t->vhost_tpgt); | |
101998f6 NB |
885 | ret = -EINVAL; |
886 | goto err; | |
057cbf49 | 887 | } |
101998f6 | 888 | tv_tpg->tv_tpg_vhost_count--; |
057cbf49 NB |
889 | vs->vs_tpg = NULL; |
890 | mutex_unlock(&vs->dev.mutex); | |
891 | ||
892 | return 0; | |
101998f6 NB |
893 | |
894 | err: | |
895 | mutex_unlock(&vs->dev.mutex); | |
896 | return ret; | |
057cbf49 NB |
897 | } |
898 | ||
899 | static int vhost_scsi_open(struct inode *inode, struct file *f) | |
900 | { | |
901 | struct vhost_scsi *s; | |
902 | int r; | |
903 | ||
904 | s = kzalloc(sizeof(*s), GFP_KERNEL); | |
905 | if (!s) | |
906 | return -ENOMEM; | |
907 | ||
908 | vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work); | |
909 | INIT_LIST_HEAD(&s->vs_completion_list); | |
910 | spin_lock_init(&s->vs_completion_lock); | |
911 | ||
101998f6 NB |
912 | s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick; |
913 | s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick; | |
914 | s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick; | |
057cbf49 NB |
915 | r = vhost_dev_init(&s->dev, s->vqs, 3); |
916 | if (r < 0) { | |
917 | kfree(s); | |
918 | return r; | |
919 | } | |
920 | ||
921 | f->private_data = s; | |
922 | return 0; | |
923 | } | |
924 | ||
925 | static int vhost_scsi_release(struct inode *inode, struct file *f) | |
926 | { | |
927 | struct vhost_scsi *s = f->private_data; | |
928 | ||
929 | if (s->vs_tpg && s->vs_tpg->tport) { | |
930 | struct vhost_scsi_target backend; | |
931 | ||
932 | memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name, | |
933 | sizeof(backend.vhost_wwpn)); | |
934 | backend.vhost_tpgt = s->vs_tpg->tport_tpgt; | |
935 | vhost_scsi_clear_endpoint(s, &backend); | |
936 | } | |
937 | ||
938 | vhost_dev_cleanup(&s->dev, false); | |
939 | kfree(s); | |
940 | return 0; | |
941 | } | |
942 | ||
101998f6 NB |
943 | static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index) |
944 | { | |
945 | vhost_poll_flush(&vs->dev.vqs[index].poll); | |
946 | } | |
947 | ||
948 | static void vhost_scsi_flush(struct vhost_scsi *vs) | |
949 | { | |
950 | vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL); | |
951 | vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT); | |
952 | vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO); | |
953 | } | |
954 | ||
057cbf49 NB |
955 | static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features) |
956 | { | |
957 | if (features & ~VHOST_FEATURES) | |
958 | return -EOPNOTSUPP; | |
959 | ||
960 | mutex_lock(&vs->dev.mutex); | |
961 | if ((features & (1 << VHOST_F_LOG_ALL)) && | |
962 | !vhost_log_access_ok(&vs->dev)) { | |
963 | mutex_unlock(&vs->dev.mutex); | |
964 | return -EFAULT; | |
965 | } | |
966 | vs->dev.acked_features = features; | |
101998f6 NB |
967 | smp_wmb(); |
968 | vhost_scsi_flush(vs); | |
057cbf49 NB |
969 | mutex_unlock(&vs->dev.mutex); |
970 | return 0; | |
971 | } | |
972 | ||
973 | static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl, | |
974 | unsigned long arg) | |
975 | { | |
976 | struct vhost_scsi *vs = f->private_data; | |
977 | struct vhost_scsi_target backend; | |
978 | void __user *argp = (void __user *)arg; | |
979 | u64 __user *featurep = argp; | |
980 | u64 features; | |
101998f6 | 981 | int r, abi_version = VHOST_SCSI_ABI_VERSION; |
057cbf49 NB |
982 | |
983 | switch (ioctl) { | |
984 | case VHOST_SCSI_SET_ENDPOINT: | |
985 | if (copy_from_user(&backend, argp, sizeof backend)) | |
986 | return -EFAULT; | |
6de7145c MT |
987 | if (backend.reserved != 0) |
988 | return -EOPNOTSUPP; | |
057cbf49 NB |
989 | |
990 | return vhost_scsi_set_endpoint(vs, &backend); | |
991 | case VHOST_SCSI_CLEAR_ENDPOINT: | |
992 | if (copy_from_user(&backend, argp, sizeof backend)) | |
993 | return -EFAULT; | |
6de7145c MT |
994 | if (backend.reserved != 0) |
995 | return -EOPNOTSUPP; | |
057cbf49 NB |
996 | |
997 | return vhost_scsi_clear_endpoint(vs, &backend); | |
998 | case VHOST_SCSI_GET_ABI_VERSION: | |
101998f6 | 999 | if (copy_to_user(argp, &abi_version, sizeof abi_version)) |
057cbf49 NB |
1000 | return -EFAULT; |
1001 | return 0; | |
1002 | case VHOST_GET_FEATURES: | |
1003 | features = VHOST_FEATURES; | |
1004 | if (copy_to_user(featurep, &features, sizeof features)) | |
1005 | return -EFAULT; | |
1006 | return 0; | |
1007 | case VHOST_SET_FEATURES: | |
1008 | if (copy_from_user(&features, featurep, sizeof features)) | |
1009 | return -EFAULT; | |
1010 | return vhost_scsi_set_features(vs, features); | |
1011 | default: | |
1012 | mutex_lock(&vs->dev.mutex); | |
1013 | r = vhost_dev_ioctl(&vs->dev, ioctl, arg); | |
1014 | mutex_unlock(&vs->dev.mutex); | |
1015 | return r; | |
1016 | } | |
1017 | } | |
1018 | ||
101998f6 NB |
1019 | #ifdef CONFIG_COMPAT |
1020 | static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl, | |
1021 | unsigned long arg) | |
1022 | { | |
1023 | return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); | |
1024 | } | |
1025 | #endif | |
1026 | ||
057cbf49 NB |
1027 | static const struct file_operations vhost_scsi_fops = { |
1028 | .owner = THIS_MODULE, | |
1029 | .release = vhost_scsi_release, | |
1030 | .unlocked_ioctl = vhost_scsi_ioctl, | |
101998f6 NB |
1031 | #ifdef CONFIG_COMPAT |
1032 | .compat_ioctl = vhost_scsi_compat_ioctl, | |
1033 | #endif | |
057cbf49 NB |
1034 | .open = vhost_scsi_open, |
1035 | .llseek = noop_llseek, | |
1036 | }; | |
1037 | ||
1038 | static struct miscdevice vhost_scsi_misc = { | |
1039 | MISC_DYNAMIC_MINOR, | |
1040 | "vhost-scsi", | |
1041 | &vhost_scsi_fops, | |
1042 | }; | |
1043 | ||
1044 | static int __init vhost_scsi_register(void) | |
1045 | { | |
1046 | return misc_register(&vhost_scsi_misc); | |
1047 | } | |
1048 | ||
1049 | static int vhost_scsi_deregister(void) | |
1050 | { | |
1051 | return misc_deregister(&vhost_scsi_misc); | |
1052 | } | |
1053 | ||
1054 | static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport) | |
1055 | { | |
1056 | switch (tport->tport_proto_id) { | |
1057 | case SCSI_PROTOCOL_SAS: | |
1058 | return "SAS"; | |
1059 | case SCSI_PROTOCOL_FCP: | |
1060 | return "FCP"; | |
1061 | case SCSI_PROTOCOL_ISCSI: | |
1062 | return "iSCSI"; | |
1063 | default: | |
1064 | break; | |
1065 | } | |
1066 | ||
1067 | return "Unknown"; | |
1068 | } | |
1069 | ||
101998f6 | 1070 | static int tcm_vhost_port_link(struct se_portal_group *se_tpg, |
057cbf49 NB |
1071 | struct se_lun *lun) |
1072 | { | |
1073 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, | |
1074 | struct tcm_vhost_tpg, se_tpg); | |
1075 | ||
101998f6 NB |
1076 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
1077 | tv_tpg->tv_tpg_port_count++; | |
1078 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
057cbf49 NB |
1079 | |
1080 | return 0; | |
1081 | } | |
1082 | ||
101998f6 | 1083 | static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg, |
057cbf49 NB |
1084 | struct se_lun *se_lun) |
1085 | { | |
1086 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, | |
1087 | struct tcm_vhost_tpg, se_tpg); | |
1088 | ||
101998f6 NB |
1089 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
1090 | tv_tpg->tv_tpg_port_count--; | |
1091 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
057cbf49 NB |
1092 | } |
1093 | ||
1094 | static struct se_node_acl *tcm_vhost_make_nodeacl( | |
1095 | struct se_portal_group *se_tpg, | |
1096 | struct config_group *group, | |
1097 | const char *name) | |
1098 | { | |
1099 | struct se_node_acl *se_nacl, *se_nacl_new; | |
1100 | struct tcm_vhost_nacl *nacl; | |
1101 | u64 wwpn = 0; | |
1102 | u32 nexus_depth; | |
1103 | ||
1104 | /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0) | |
1105 | return ERR_PTR(-EINVAL); */ | |
1106 | se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg); | |
1107 | if (!se_nacl_new) | |
1108 | return ERR_PTR(-ENOMEM); | |
1109 | ||
1110 | nexus_depth = 1; | |
1111 | /* | |
1112 | * se_nacl_new may be released by core_tpg_add_initiator_node_acl() | |
1113 | * when converting a NodeACL from demo mode -> explict | |
1114 | */ | |
1115 | se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new, | |
1116 | name, nexus_depth); | |
1117 | if (IS_ERR(se_nacl)) { | |
1118 | tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new); | |
1119 | return se_nacl; | |
1120 | } | |
1121 | /* | |
1122 | * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN | |
1123 | */ | |
1124 | nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl); | |
1125 | nacl->iport_wwpn = wwpn; | |
1126 | ||
1127 | return se_nacl; | |
1128 | } | |
1129 | ||
1130 | static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl) | |
1131 | { | |
1132 | struct tcm_vhost_nacl *nacl = container_of(se_acl, | |
1133 | struct tcm_vhost_nacl, se_node_acl); | |
1134 | core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1); | |
1135 | kfree(nacl); | |
1136 | } | |
1137 | ||
101998f6 | 1138 | static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg, |
057cbf49 NB |
1139 | const char *name) |
1140 | { | |
1141 | struct se_portal_group *se_tpg; | |
1142 | struct tcm_vhost_nexus *tv_nexus; | |
1143 | ||
1144 | mutex_lock(&tv_tpg->tv_tpg_mutex); | |
1145 | if (tv_tpg->tpg_nexus) { | |
1146 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1147 | pr_debug("tv_tpg->tpg_nexus already exists\n"); | |
1148 | return -EEXIST; | |
1149 | } | |
1150 | se_tpg = &tv_tpg->se_tpg; | |
1151 | ||
1152 | tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL); | |
1153 | if (!tv_nexus) { | |
1154 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1155 | pr_err("Unable to allocate struct tcm_vhost_nexus\n"); | |
1156 | return -ENOMEM; | |
1157 | } | |
1158 | /* | |
1159 | * Initialize the struct se_session pointer | |
1160 | */ | |
1161 | tv_nexus->tvn_se_sess = transport_init_session(); | |
1162 | if (IS_ERR(tv_nexus->tvn_se_sess)) { | |
1163 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1164 | kfree(tv_nexus); | |
1165 | return -ENOMEM; | |
1166 | } | |
1167 | /* | |
1168 | * Since we are running in 'demo mode' this call with generate a | |
1169 | * struct se_node_acl for the tcm_vhost struct se_portal_group with | |
1170 | * the SCSI Initiator port name of the passed configfs group 'name'. | |
1171 | */ | |
1172 | tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( | |
1173 | se_tpg, (unsigned char *)name); | |
1174 | if (!tv_nexus->tvn_se_sess->se_node_acl) { | |
1175 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1176 | pr_debug("core_tpg_check_initiator_node_acl() failed" | |
1177 | " for %s\n", name); | |
1178 | transport_free_session(tv_nexus->tvn_se_sess); | |
1179 | kfree(tv_nexus); | |
1180 | return -ENOMEM; | |
1181 | } | |
1182 | /* | |
101998f6 | 1183 | * Now register the TCM vhost virtual I_T Nexus as active with the |
057cbf49 NB |
1184 | * call to __transport_register_session() |
1185 | */ | |
1186 | __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, | |
1187 | tv_nexus->tvn_se_sess, tv_nexus); | |
1188 | tv_tpg->tpg_nexus = tv_nexus; | |
1189 | ||
1190 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1191 | return 0; | |
1192 | } | |
1193 | ||
101998f6 | 1194 | static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg) |
057cbf49 NB |
1195 | { |
1196 | struct se_session *se_sess; | |
1197 | struct tcm_vhost_nexus *tv_nexus; | |
1198 | ||
1199 | mutex_lock(&tpg->tv_tpg_mutex); | |
1200 | tv_nexus = tpg->tpg_nexus; | |
1201 | if (!tv_nexus) { | |
1202 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1203 | return -ENODEV; | |
1204 | } | |
1205 | ||
1206 | se_sess = tv_nexus->tvn_se_sess; | |
1207 | if (!se_sess) { | |
1208 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1209 | return -ENODEV; | |
1210 | } | |
1211 | ||
101998f6 | 1212 | if (tpg->tv_tpg_port_count != 0) { |
057cbf49 | 1213 | mutex_unlock(&tpg->tv_tpg_mutex); |
101998f6 | 1214 | pr_err("Unable to remove TCM_vhost I_T Nexus with" |
057cbf49 | 1215 | " active TPG port count: %d\n", |
101998f6 NB |
1216 | tpg->tv_tpg_port_count); |
1217 | return -EBUSY; | |
057cbf49 NB |
1218 | } |
1219 | ||
101998f6 | 1220 | if (tpg->tv_tpg_vhost_count != 0) { |
057cbf49 | 1221 | mutex_unlock(&tpg->tv_tpg_mutex); |
101998f6 | 1222 | pr_err("Unable to remove TCM_vhost I_T Nexus with" |
057cbf49 | 1223 | " active TPG vhost count: %d\n", |
101998f6 NB |
1224 | tpg->tv_tpg_vhost_count); |
1225 | return -EBUSY; | |
057cbf49 NB |
1226 | } |
1227 | ||
101998f6 | 1228 | pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated" |
057cbf49 NB |
1229 | " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport), |
1230 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); | |
1231 | /* | |
101998f6 | 1232 | * Release the SCSI I_T Nexus to the emulated vhost Target Port |
057cbf49 NB |
1233 | */ |
1234 | transport_deregister_session(tv_nexus->tvn_se_sess); | |
1235 | tpg->tpg_nexus = NULL; | |
1236 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1237 | ||
1238 | kfree(tv_nexus); | |
1239 | return 0; | |
1240 | } | |
1241 | ||
101998f6 | 1242 | static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg, |
057cbf49 NB |
1243 | char *page) |
1244 | { | |
1245 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, | |
1246 | struct tcm_vhost_tpg, se_tpg); | |
1247 | struct tcm_vhost_nexus *tv_nexus; | |
1248 | ssize_t ret; | |
1249 | ||
1250 | mutex_lock(&tv_tpg->tv_tpg_mutex); | |
1251 | tv_nexus = tv_tpg->tpg_nexus; | |
1252 | if (!tv_nexus) { | |
1253 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1254 | return -ENODEV; | |
1255 | } | |
1256 | ret = snprintf(page, PAGE_SIZE, "%s\n", | |
1257 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); | |
1258 | mutex_unlock(&tv_tpg->tv_tpg_mutex); | |
1259 | ||
1260 | return ret; | |
1261 | } | |
1262 | ||
101998f6 | 1263 | static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg, |
057cbf49 NB |
1264 | const char *page, |
1265 | size_t count) | |
1266 | { | |
1267 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, | |
1268 | struct tcm_vhost_tpg, se_tpg); | |
1269 | struct tcm_vhost_tport *tport_wwn = tv_tpg->tport; | |
1270 | unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr; | |
1271 | int ret; | |
1272 | /* | |
1273 | * Shutdown the active I_T nexus if 'NULL' is passed.. | |
1274 | */ | |
1275 | if (!strncmp(page, "NULL", 4)) { | |
1276 | ret = tcm_vhost_drop_nexus(tv_tpg); | |
1277 | return (!ret) ? count : ret; | |
1278 | } | |
1279 | /* | |
1280 | * Otherwise make sure the passed virtual Initiator port WWN matches | |
1281 | * the fabric protocol_id set in tcm_vhost_make_tport(), and call | |
1282 | * tcm_vhost_make_nexus(). | |
1283 | */ | |
1284 | if (strlen(page) >= TCM_VHOST_NAMELEN) { | |
1285 | pr_err("Emulated NAA Sas Address: %s, exceeds" | |
1286 | " max: %d\n", page, TCM_VHOST_NAMELEN); | |
1287 | return -EINVAL; | |
1288 | } | |
1289 | snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page); | |
1290 | ||
1291 | ptr = strstr(i_port, "naa."); | |
1292 | if (ptr) { | |
1293 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { | |
1294 | pr_err("Passed SAS Initiator Port %s does not" | |
1295 | " match target port protoid: %s\n", i_port, | |
1296 | tcm_vhost_dump_proto_id(tport_wwn)); | |
1297 | return -EINVAL; | |
1298 | } | |
1299 | port_ptr = &i_port[0]; | |
1300 | goto check_newline; | |
1301 | } | |
1302 | ptr = strstr(i_port, "fc."); | |
1303 | if (ptr) { | |
1304 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { | |
1305 | pr_err("Passed FCP Initiator Port %s does not" | |
1306 | " match target port protoid: %s\n", i_port, | |
1307 | tcm_vhost_dump_proto_id(tport_wwn)); | |
1308 | return -EINVAL; | |
1309 | } | |
1310 | port_ptr = &i_port[3]; /* Skip over "fc." */ | |
1311 | goto check_newline; | |
1312 | } | |
1313 | ptr = strstr(i_port, "iqn."); | |
1314 | if (ptr) { | |
1315 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { | |
1316 | pr_err("Passed iSCSI Initiator Port %s does not" | |
1317 | " match target port protoid: %s\n", i_port, | |
1318 | tcm_vhost_dump_proto_id(tport_wwn)); | |
1319 | return -EINVAL; | |
1320 | } | |
1321 | port_ptr = &i_port[0]; | |
1322 | goto check_newline; | |
1323 | } | |
1324 | pr_err("Unable to locate prefix for emulated Initiator Port:" | |
1325 | " %s\n", i_port); | |
1326 | return -EINVAL; | |
1327 | /* | |
1328 | * Clear any trailing newline for the NAA WWN | |
1329 | */ | |
1330 | check_newline: | |
1331 | if (i_port[strlen(i_port)-1] == '\n') | |
1332 | i_port[strlen(i_port)-1] = '\0'; | |
1333 | ||
1334 | ret = tcm_vhost_make_nexus(tv_tpg, port_ptr); | |
1335 | if (ret < 0) | |
1336 | return ret; | |
1337 | ||
1338 | return count; | |
1339 | } | |
1340 | ||
1341 | TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR); | |
1342 | ||
1343 | static struct configfs_attribute *tcm_vhost_tpg_attrs[] = { | |
1344 | &tcm_vhost_tpg_nexus.attr, | |
1345 | NULL, | |
1346 | }; | |
1347 | ||
101998f6 | 1348 | static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn, |
057cbf49 NB |
1349 | struct config_group *group, |
1350 | const char *name) | |
1351 | { | |
1352 | struct tcm_vhost_tport *tport = container_of(wwn, | |
1353 | struct tcm_vhost_tport, tport_wwn); | |
1354 | ||
1355 | struct tcm_vhost_tpg *tpg; | |
1356 | unsigned long tpgt; | |
1357 | int ret; | |
1358 | ||
1359 | if (strstr(name, "tpgt_") != name) | |
1360 | return ERR_PTR(-EINVAL); | |
1361 | if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX) | |
1362 | return ERR_PTR(-EINVAL); | |
1363 | ||
1364 | tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL); | |
1365 | if (!tpg) { | |
1366 | pr_err("Unable to allocate struct tcm_vhost_tpg"); | |
1367 | return ERR_PTR(-ENOMEM); | |
1368 | } | |
1369 | mutex_init(&tpg->tv_tpg_mutex); | |
1370 | INIT_LIST_HEAD(&tpg->tv_tpg_list); | |
1371 | tpg->tport = tport; | |
1372 | tpg->tport_tpgt = tpgt; | |
1373 | ||
1374 | ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn, | |
1375 | &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL); | |
1376 | if (ret < 0) { | |
1377 | kfree(tpg); | |
1378 | return NULL; | |
1379 | } | |
1380 | mutex_lock(&tcm_vhost_mutex); | |
1381 | list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list); | |
1382 | mutex_unlock(&tcm_vhost_mutex); | |
1383 | ||
1384 | return &tpg->se_tpg; | |
1385 | } | |
1386 | ||
1387 | static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg) | |
1388 | { | |
1389 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
1390 | struct tcm_vhost_tpg, se_tpg); | |
1391 | ||
1392 | mutex_lock(&tcm_vhost_mutex); | |
1393 | list_del(&tpg->tv_tpg_list); | |
1394 | mutex_unlock(&tcm_vhost_mutex); | |
1395 | /* | |
101998f6 | 1396 | * Release the virtual I_T Nexus for this vhost TPG |
057cbf49 NB |
1397 | */ |
1398 | tcm_vhost_drop_nexus(tpg); | |
1399 | /* | |
1400 | * Deregister the se_tpg from TCM.. | |
1401 | */ | |
1402 | core_tpg_deregister(se_tpg); | |
1403 | kfree(tpg); | |
1404 | } | |
1405 | ||
101998f6 | 1406 | static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf, |
057cbf49 NB |
1407 | struct config_group *group, |
1408 | const char *name) | |
1409 | { | |
1410 | struct tcm_vhost_tport *tport; | |
1411 | char *ptr; | |
1412 | u64 wwpn = 0; | |
1413 | int off = 0; | |
1414 | ||
1415 | /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0) | |
1416 | return ERR_PTR(-EINVAL); */ | |
1417 | ||
1418 | tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL); | |
1419 | if (!tport) { | |
1420 | pr_err("Unable to allocate struct tcm_vhost_tport"); | |
1421 | return ERR_PTR(-ENOMEM); | |
1422 | } | |
1423 | tport->tport_wwpn = wwpn; | |
1424 | /* | |
1425 | * Determine the emulated Protocol Identifier and Target Port Name | |
1426 | * based on the incoming configfs directory name. | |
1427 | */ | |
1428 | ptr = strstr(name, "naa."); | |
1429 | if (ptr) { | |
1430 | tport->tport_proto_id = SCSI_PROTOCOL_SAS; | |
1431 | goto check_len; | |
1432 | } | |
1433 | ptr = strstr(name, "fc."); | |
1434 | if (ptr) { | |
1435 | tport->tport_proto_id = SCSI_PROTOCOL_FCP; | |
1436 | off = 3; /* Skip over "fc." */ | |
1437 | goto check_len; | |
1438 | } | |
1439 | ptr = strstr(name, "iqn."); | |
1440 | if (ptr) { | |
1441 | tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; | |
1442 | goto check_len; | |
1443 | } | |
1444 | ||
1445 | pr_err("Unable to locate prefix for emulated Target Port:" | |
1446 | " %s\n", name); | |
1447 | kfree(tport); | |
1448 | return ERR_PTR(-EINVAL); | |
1449 | ||
1450 | check_len: | |
1451 | if (strlen(name) >= TCM_VHOST_NAMELEN) { | |
1452 | pr_err("Emulated %s Address: %s, exceeds" | |
1453 | " max: %d\n", name, tcm_vhost_dump_proto_id(tport), | |
1454 | TCM_VHOST_NAMELEN); | |
1455 | kfree(tport); | |
1456 | return ERR_PTR(-EINVAL); | |
1457 | } | |
1458 | snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]); | |
1459 | ||
1460 | pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target" | |
1461 | " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name); | |
1462 | ||
1463 | return &tport->tport_wwn; | |
1464 | } | |
1465 | ||
1466 | static void tcm_vhost_drop_tport(struct se_wwn *wwn) | |
1467 | { | |
1468 | struct tcm_vhost_tport *tport = container_of(wwn, | |
1469 | struct tcm_vhost_tport, tport_wwn); | |
1470 | ||
1471 | pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target" | |
1472 | " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), | |
1473 | tport->tport_name); | |
1474 | ||
1475 | kfree(tport); | |
1476 | } | |
1477 | ||
1478 | static ssize_t tcm_vhost_wwn_show_attr_version( | |
1479 | struct target_fabric_configfs *tf, | |
1480 | char *page) | |
1481 | { | |
1482 | return sprintf(page, "TCM_VHOST fabric module %s on %s/%s" | |
1483 | "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, | |
1484 | utsname()->machine); | |
1485 | } | |
1486 | ||
1487 | TF_WWN_ATTR_RO(tcm_vhost, version); | |
1488 | ||
1489 | static struct configfs_attribute *tcm_vhost_wwn_attrs[] = { | |
1490 | &tcm_vhost_wwn_version.attr, | |
1491 | NULL, | |
1492 | }; | |
1493 | ||
1494 | static struct target_core_fabric_ops tcm_vhost_ops = { | |
1495 | .get_fabric_name = tcm_vhost_get_fabric_name, | |
1496 | .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident, | |
1497 | .tpg_get_wwn = tcm_vhost_get_fabric_wwn, | |
1498 | .tpg_get_tag = tcm_vhost_get_tag, | |
1499 | .tpg_get_default_depth = tcm_vhost_get_default_depth, | |
1500 | .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id, | |
1501 | .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len, | |
1502 | .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id, | |
1503 | .tpg_check_demo_mode = tcm_vhost_check_true, | |
1504 | .tpg_check_demo_mode_cache = tcm_vhost_check_true, | |
1505 | .tpg_check_demo_mode_write_protect = tcm_vhost_check_false, | |
1506 | .tpg_check_prod_mode_write_protect = tcm_vhost_check_false, | |
1507 | .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl, | |
1508 | .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl, | |
1509 | .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index, | |
1510 | .release_cmd = tcm_vhost_release_cmd, | |
1511 | .shutdown_session = tcm_vhost_shutdown_session, | |
1512 | .close_session = tcm_vhost_close_session, | |
1513 | .sess_get_index = tcm_vhost_sess_get_index, | |
1514 | .sess_get_initiator_sid = NULL, | |
1515 | .write_pending = tcm_vhost_write_pending, | |
1516 | .write_pending_status = tcm_vhost_write_pending_status, | |
1517 | .set_default_node_attributes = tcm_vhost_set_default_node_attrs, | |
1518 | .get_task_tag = tcm_vhost_get_task_tag, | |
1519 | .get_cmd_state = tcm_vhost_get_cmd_state, | |
1520 | .queue_data_in = tcm_vhost_queue_data_in, | |
1521 | .queue_status = tcm_vhost_queue_status, | |
1522 | .queue_tm_rsp = tcm_vhost_queue_tm_rsp, | |
057cbf49 NB |
1523 | /* |
1524 | * Setup callers for generic logic in target_core_fabric_configfs.c | |
1525 | */ | |
1526 | .fabric_make_wwn = tcm_vhost_make_tport, | |
1527 | .fabric_drop_wwn = tcm_vhost_drop_tport, | |
1528 | .fabric_make_tpg = tcm_vhost_make_tpg, | |
1529 | .fabric_drop_tpg = tcm_vhost_drop_tpg, | |
1530 | .fabric_post_link = tcm_vhost_port_link, | |
1531 | .fabric_pre_unlink = tcm_vhost_port_unlink, | |
1532 | .fabric_make_np = NULL, | |
1533 | .fabric_drop_np = NULL, | |
1534 | .fabric_make_nodeacl = tcm_vhost_make_nodeacl, | |
1535 | .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl, | |
1536 | }; | |
1537 | ||
1538 | static int tcm_vhost_register_configfs(void) | |
1539 | { | |
1540 | struct target_fabric_configfs *fabric; | |
1541 | int ret; | |
1542 | ||
1543 | pr_debug("TCM_VHOST fabric module %s on %s/%s" | |
1544 | " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, | |
1545 | utsname()->machine); | |
1546 | /* | |
1547 | * Register the top level struct config_item_type with TCM core | |
1548 | */ | |
1549 | fabric = target_fabric_configfs_init(THIS_MODULE, "vhost"); | |
1550 | if (IS_ERR(fabric)) { | |
1551 | pr_err("target_fabric_configfs_init() failed\n"); | |
1552 | return PTR_ERR(fabric); | |
1553 | } | |
1554 | /* | |
1555 | * Setup fabric->tf_ops from our local tcm_vhost_ops | |
1556 | */ | |
1557 | fabric->tf_ops = tcm_vhost_ops; | |
1558 | /* | |
1559 | * Setup default attribute lists for various fabric->tf_cit_tmpl | |
1560 | */ | |
1561 | TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs; | |
1562 | TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs; | |
1563 | TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL; | |
1564 | TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL; | |
1565 | TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL; | |
1566 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL; | |
1567 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; | |
1568 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL; | |
1569 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL; | |
1570 | /* | |
1571 | * Register the fabric for use within TCM | |
1572 | */ | |
1573 | ret = target_fabric_configfs_register(fabric); | |
1574 | if (ret < 0) { | |
1575 | pr_err("target_fabric_configfs_register() failed" | |
1576 | " for TCM_VHOST\n"); | |
1577 | return ret; | |
1578 | } | |
1579 | /* | |
1580 | * Setup our local pointer to *fabric | |
1581 | */ | |
1582 | tcm_vhost_fabric_configfs = fabric; | |
1583 | pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n"); | |
1584 | return 0; | |
1585 | }; | |
1586 | ||
1587 | static void tcm_vhost_deregister_configfs(void) | |
1588 | { | |
1589 | if (!tcm_vhost_fabric_configfs) | |
1590 | return; | |
1591 | ||
1592 | target_fabric_configfs_deregister(tcm_vhost_fabric_configfs); | |
1593 | tcm_vhost_fabric_configfs = NULL; | |
1594 | pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n"); | |
1595 | }; | |
1596 | ||
1597 | static int __init tcm_vhost_init(void) | |
1598 | { | |
1599 | int ret = -ENOMEM; | |
101998f6 NB |
1600 | /* |
1601 | * Use our own dedicated workqueue for submitting I/O into | |
1602 | * target core to avoid contention within system_wq. | |
1603 | */ | |
057cbf49 NB |
1604 | tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0); |
1605 | if (!tcm_vhost_workqueue) | |
1606 | goto out; | |
1607 | ||
1608 | ret = vhost_scsi_register(); | |
1609 | if (ret < 0) | |
1610 | goto out_destroy_workqueue; | |
1611 | ||
1612 | ret = tcm_vhost_register_configfs(); | |
1613 | if (ret < 0) | |
1614 | goto out_vhost_scsi_deregister; | |
1615 | ||
1616 | return 0; | |
1617 | ||
1618 | out_vhost_scsi_deregister: | |
1619 | vhost_scsi_deregister(); | |
1620 | out_destroy_workqueue: | |
1621 | destroy_workqueue(tcm_vhost_workqueue); | |
1622 | out: | |
1623 | return ret; | |
1624 | }; | |
1625 | ||
1626 | static void tcm_vhost_exit(void) | |
1627 | { | |
1628 | tcm_vhost_deregister_configfs(); | |
1629 | vhost_scsi_deregister(); | |
1630 | destroy_workqueue(tcm_vhost_workqueue); | |
1631 | }; | |
1632 | ||
1633 | MODULE_DESCRIPTION("TCM_VHOST series fabric driver"); | |
1634 | MODULE_LICENSE("GPL"); | |
1635 | module_init(tcm_vhost_init); | |
1636 | module_exit(tcm_vhost_exit); |