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