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