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> | |
057cbf49 | 48 | #include <linux/virtio_scsi.h> |
9d6064a3 | 49 | #include <linux/llist.h> |
1b7f390e | 50 | #include <linux/bitmap.h> |
057cbf49 NB |
51 | |
52 | #include "vhost.c" | |
53 | #include "vhost.h" | |
5012a3a3 MT |
54 | |
55 | #define TCM_VHOST_VERSION "v0.1" | |
56 | #define TCM_VHOST_NAMELEN 256 | |
57 | #define TCM_VHOST_MAX_CDB_SIZE 32 | |
58 | ||
59 | struct vhost_scsi_inflight { | |
60 | /* Wait for the flush operation to finish */ | |
61 | struct completion comp; | |
62 | /* Refcount for the inflight reqs */ | |
63 | struct kref kref; | |
64 | }; | |
65 | ||
66 | struct tcm_vhost_cmd { | |
67 | /* Descriptor from vhost_get_vq_desc() for virt_queue segment */ | |
68 | int tvc_vq_desc; | |
69 | /* virtio-scsi initiator task attribute */ | |
70 | int tvc_task_attr; | |
71 | /* virtio-scsi initiator data direction */ | |
72 | enum dma_data_direction tvc_data_direction; | |
73 | /* Expected data transfer length from virtio-scsi header */ | |
74 | u32 tvc_exp_data_len; | |
75 | /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */ | |
76 | u64 tvc_tag; | |
77 | /* The number of scatterlists associated with this cmd */ | |
78 | u32 tvc_sgl_count; | |
79 | /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */ | |
80 | u32 tvc_lun; | |
81 | /* Pointer to the SGL formatted memory from virtio-scsi */ | |
82 | struct scatterlist *tvc_sgl; | |
83 | /* Pointer to response */ | |
84 | struct virtio_scsi_cmd_resp __user *tvc_resp; | |
85 | /* Pointer to vhost_scsi for our device */ | |
86 | struct vhost_scsi *tvc_vhost; | |
87 | /* Pointer to vhost_virtqueue for the cmd */ | |
88 | struct vhost_virtqueue *tvc_vq; | |
89 | /* Pointer to vhost nexus memory */ | |
90 | struct tcm_vhost_nexus *tvc_nexus; | |
91 | /* The TCM I/O descriptor that is accessed via container_of() */ | |
92 | struct se_cmd tvc_se_cmd; | |
93 | /* work item used for cmwq dispatch to tcm_vhost_submission_work() */ | |
94 | struct work_struct work; | |
95 | /* Copy of the incoming SCSI command descriptor block (CDB) */ | |
96 | unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE]; | |
97 | /* Sense buffer that will be mapped into outgoing status */ | |
98 | unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER]; | |
99 | /* Completed commands list, serviced from vhost worker thread */ | |
100 | struct llist_node tvc_completion_list; | |
101 | /* Used to track inflight cmd */ | |
102 | struct vhost_scsi_inflight *inflight; | |
103 | }; | |
104 | ||
105 | struct tcm_vhost_nexus { | |
106 | /* Pointer to TCM session for I_T Nexus */ | |
107 | struct se_session *tvn_se_sess; | |
108 | }; | |
109 | ||
110 | struct tcm_vhost_nacl { | |
111 | /* Binary World Wide unique Port Name for Vhost Initiator port */ | |
112 | u64 iport_wwpn; | |
113 | /* ASCII formatted WWPN for Sas Initiator port */ | |
114 | char iport_name[TCM_VHOST_NAMELEN]; | |
115 | /* Returned by tcm_vhost_make_nodeacl() */ | |
116 | struct se_node_acl se_node_acl; | |
117 | }; | |
118 | ||
5012a3a3 MT |
119 | struct tcm_vhost_tpg { |
120 | /* Vhost port target portal group tag for TCM */ | |
121 | u16 tport_tpgt; | |
122 | /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */ | |
123 | int tv_tpg_port_count; | |
124 | /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */ | |
125 | int tv_tpg_vhost_count; | |
126 | /* list for tcm_vhost_list */ | |
127 | struct list_head tv_tpg_list; | |
128 | /* Used to protect access for tpg_nexus */ | |
129 | struct mutex tv_tpg_mutex; | |
130 | /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */ | |
131 | struct tcm_vhost_nexus *tpg_nexus; | |
132 | /* Pointer back to tcm_vhost_tport */ | |
133 | struct tcm_vhost_tport *tport; | |
134 | /* Returned by tcm_vhost_make_tpg() */ | |
135 | struct se_portal_group se_tpg; | |
136 | /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */ | |
137 | struct vhost_scsi *vhost_scsi; | |
138 | }; | |
139 | ||
140 | struct tcm_vhost_tport { | |
141 | /* SCSI protocol the tport is providing */ | |
142 | u8 tport_proto_id; | |
143 | /* Binary World Wide unique Port Name for Vhost Target port */ | |
144 | u64 tport_wwpn; | |
145 | /* ASCII formatted WWPN for Vhost Target port */ | |
146 | char tport_name[TCM_VHOST_NAMELEN]; | |
147 | /* Returned by tcm_vhost_make_tport() */ | |
148 | struct se_wwn tport_wwn; | |
149 | }; | |
150 | ||
151 | struct tcm_vhost_evt { | |
152 | /* event to be sent to guest */ | |
153 | struct virtio_scsi_event event; | |
154 | /* event list, serviced from vhost worker thread */ | |
155 | struct llist_node list; | |
156 | }; | |
057cbf49 | 157 | |
101998f6 NB |
158 | enum { |
159 | VHOST_SCSI_VQ_CTL = 0, | |
160 | VHOST_SCSI_VQ_EVT = 1, | |
161 | VHOST_SCSI_VQ_IO = 2, | |
162 | }; | |
163 | ||
5dade710 | 164 | enum { |
a18cc421 | 165 | VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
5dade710 NB |
166 | }; |
167 | ||
1b7f390e AH |
168 | #define VHOST_SCSI_MAX_TARGET 256 |
169 | #define VHOST_SCSI_MAX_VQ 128 | |
a6c9af87 | 170 | #define VHOST_SCSI_MAX_EVENT 128 |
67e18cf9 | 171 | |
3ab2e420 AH |
172 | struct vhost_scsi_virtqueue { |
173 | struct vhost_virtqueue vq; | |
3dfbff32 MT |
174 | /* |
175 | * Reference counting for inflight reqs, used for flush operation. At | |
176 | * each time, one reference tracks new commands submitted, while we | |
177 | * wait for another one to reach 0. | |
178 | */ | |
f2f0173d | 179 | struct vhost_scsi_inflight inflights[2]; |
3dfbff32 MT |
180 | /* |
181 | * Indicate current inflight in use, protected by vq->mutex. | |
182 | * Writers must also take dev mutex and flush under it. | |
183 | */ | |
f2f0173d | 184 | int inflight_idx; |
3ab2e420 AH |
185 | }; |
186 | ||
057cbf49 | 187 | struct vhost_scsi { |
67e18cf9 | 188 | /* Protected by vhost_scsi->dev.mutex */ |
4f7f46d3 | 189 | struct tcm_vhost_tpg **vs_tpg; |
67e18cf9 | 190 | char vs_vhost_wwpn[TRANSPORT_IQN_LEN]; |
67e18cf9 | 191 | |
057cbf49 | 192 | struct vhost_dev dev; |
3ab2e420 | 193 | struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ]; |
057cbf49 NB |
194 | |
195 | struct vhost_work vs_completion_work; /* cmd completion work item */ | |
9d6064a3 | 196 | struct llist_head vs_completion_list; /* cmd completion queue */ |
a6c9af87 AH |
197 | |
198 | struct vhost_work vs_event_work; /* evt injection work item */ | |
199 | struct llist_head vs_event_list; /* evt injection queue */ | |
200 | ||
201 | bool vs_events_missed; /* any missed events, protected by vq->mutex */ | |
202 | int vs_events_nr; /* num of pending events, protected by vq->mutex */ | |
057cbf49 NB |
203 | }; |
204 | ||
205 | /* Local pointer to allocated TCM configfs fabric module */ | |
206 | static struct target_fabric_configfs *tcm_vhost_fabric_configfs; | |
207 | ||
208 | static struct workqueue_struct *tcm_vhost_workqueue; | |
209 | ||
210 | /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */ | |
211 | static DEFINE_MUTEX(tcm_vhost_mutex); | |
212 | static LIST_HEAD(tcm_vhost_list); | |
213 | ||
765b34fd AH |
214 | static int iov_num_pages(struct iovec *iov) |
215 | { | |
216 | return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) - | |
217 | ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT; | |
218 | } | |
219 | ||
f2f0173d AH |
220 | void tcm_vhost_done_inflight(struct kref *kref) |
221 | { | |
222 | struct vhost_scsi_inflight *inflight; | |
223 | ||
224 | inflight = container_of(kref, struct vhost_scsi_inflight, kref); | |
225 | complete(&inflight->comp); | |
226 | } | |
227 | ||
228 | static void tcm_vhost_init_inflight(struct vhost_scsi *vs, | |
229 | struct vhost_scsi_inflight *old_inflight[]) | |
230 | { | |
231 | struct vhost_scsi_inflight *new_inflight; | |
232 | struct vhost_virtqueue *vq; | |
233 | int idx, i; | |
234 | ||
235 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { | |
236 | vq = &vs->vqs[i].vq; | |
237 | ||
238 | mutex_lock(&vq->mutex); | |
239 | ||
240 | /* store old infight */ | |
241 | idx = vs->vqs[i].inflight_idx; | |
242 | if (old_inflight) | |
243 | old_inflight[i] = &vs->vqs[i].inflights[idx]; | |
244 | ||
245 | /* setup new infight */ | |
246 | vs->vqs[i].inflight_idx = idx ^ 1; | |
247 | new_inflight = &vs->vqs[i].inflights[idx ^ 1]; | |
248 | kref_init(&new_inflight->kref); | |
249 | init_completion(&new_inflight->comp); | |
250 | ||
251 | mutex_unlock(&vq->mutex); | |
252 | } | |
253 | } | |
254 | ||
255 | static struct vhost_scsi_inflight * | |
256 | tcm_vhost_get_inflight(struct vhost_virtqueue *vq) | |
257 | { | |
258 | struct vhost_scsi_inflight *inflight; | |
259 | struct vhost_scsi_virtqueue *svq; | |
260 | ||
261 | svq = container_of(vq, struct vhost_scsi_virtqueue, vq); | |
262 | inflight = &svq->inflights[svq->inflight_idx]; | |
263 | kref_get(&inflight->kref); | |
264 | ||
265 | return inflight; | |
266 | } | |
267 | ||
268 | static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight) | |
269 | { | |
270 | kref_put(&inflight->kref, tcm_vhost_done_inflight); | |
271 | } | |
272 | ||
057cbf49 NB |
273 | static int tcm_vhost_check_true(struct se_portal_group *se_tpg) |
274 | { | |
275 | return 1; | |
276 | } | |
277 | ||
278 | static int tcm_vhost_check_false(struct se_portal_group *se_tpg) | |
279 | { | |
280 | return 0; | |
281 | } | |
282 | ||
283 | static char *tcm_vhost_get_fabric_name(void) | |
284 | { | |
285 | return "vhost"; | |
286 | } | |
287 | ||
288 | static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg) | |
289 | { | |
290 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
291 | struct tcm_vhost_tpg, se_tpg); | |
292 | struct tcm_vhost_tport *tport = tpg->tport; | |
293 | ||
294 | switch (tport->tport_proto_id) { | |
295 | case SCSI_PROTOCOL_SAS: | |
296 | return sas_get_fabric_proto_ident(se_tpg); | |
297 | case SCSI_PROTOCOL_FCP: | |
298 | return fc_get_fabric_proto_ident(se_tpg); | |
299 | case SCSI_PROTOCOL_ISCSI: | |
300 | return iscsi_get_fabric_proto_ident(se_tpg); | |
301 | default: | |
302 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
303 | " SAS emulation\n", tport->tport_proto_id); | |
304 | break; | |
305 | } | |
306 | ||
307 | return sas_get_fabric_proto_ident(se_tpg); | |
308 | } | |
309 | ||
310 | static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg) | |
311 | { | |
312 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
313 | struct tcm_vhost_tpg, se_tpg); | |
314 | struct tcm_vhost_tport *tport = tpg->tport; | |
315 | ||
316 | return &tport->tport_name[0]; | |
317 | } | |
318 | ||
319 | static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg) | |
320 | { | |
321 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
322 | struct tcm_vhost_tpg, se_tpg); | |
323 | return tpg->tport_tpgt; | |
324 | } | |
325 | ||
326 | static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg) | |
327 | { | |
328 | return 1; | |
329 | } | |
330 | ||
683bd967 AH |
331 | static u32 |
332 | tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg, | |
333 | struct se_node_acl *se_nacl, | |
334 | struct t10_pr_registration *pr_reg, | |
335 | int *format_code, | |
336 | unsigned char *buf) | |
057cbf49 NB |
337 | { |
338 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
339 | struct tcm_vhost_tpg, se_tpg); | |
340 | struct tcm_vhost_tport *tport = tpg->tport; | |
341 | ||
342 | switch (tport->tport_proto_id) { | |
343 | case SCSI_PROTOCOL_SAS: | |
344 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
345 | format_code, buf); | |
346 | case SCSI_PROTOCOL_FCP: | |
347 | return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
348 | format_code, buf); | |
349 | case SCSI_PROTOCOL_ISCSI: | |
350 | return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
351 | format_code, buf); | |
352 | default: | |
353 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
354 | " SAS emulation\n", tport->tport_proto_id); | |
355 | break; | |
356 | } | |
357 | ||
358 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, | |
359 | format_code, buf); | |
360 | } | |
361 | ||
683bd967 AH |
362 | static u32 |
363 | tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg, | |
364 | struct se_node_acl *se_nacl, | |
365 | struct t10_pr_registration *pr_reg, | |
366 | int *format_code) | |
057cbf49 NB |
367 | { |
368 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
369 | struct tcm_vhost_tpg, se_tpg); | |
370 | struct tcm_vhost_tport *tport = tpg->tport; | |
371 | ||
372 | switch (tport->tport_proto_id) { | |
373 | case SCSI_PROTOCOL_SAS: | |
374 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
375 | format_code); | |
376 | case SCSI_PROTOCOL_FCP: | |
377 | return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
378 | format_code); | |
379 | case SCSI_PROTOCOL_ISCSI: | |
380 | return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
381 | format_code); | |
382 | default: | |
383 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
384 | " SAS emulation\n", tport->tport_proto_id); | |
385 | break; | |
386 | } | |
387 | ||
388 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, | |
389 | format_code); | |
390 | } | |
391 | ||
683bd967 AH |
392 | static char * |
393 | tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg, | |
394 | const char *buf, | |
395 | u32 *out_tid_len, | |
396 | char **port_nexus_ptr) | |
057cbf49 NB |
397 | { |
398 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
399 | struct tcm_vhost_tpg, se_tpg); | |
400 | struct tcm_vhost_tport *tport = tpg->tport; | |
401 | ||
402 | switch (tport->tport_proto_id) { | |
403 | case SCSI_PROTOCOL_SAS: | |
404 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
405 | port_nexus_ptr); | |
406 | case SCSI_PROTOCOL_FCP: | |
407 | return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
408 | port_nexus_ptr); | |
409 | case SCSI_PROTOCOL_ISCSI: | |
410 | return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
411 | port_nexus_ptr); | |
412 | default: | |
413 | pr_err("Unknown tport_proto_id: 0x%02x, using" | |
414 | " SAS emulation\n", tport->tport_proto_id); | |
415 | break; | |
416 | } | |
417 | ||
418 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, | |
419 | port_nexus_ptr); | |
420 | } | |
421 | ||
683bd967 AH |
422 | static struct se_node_acl * |
423 | tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg) | |
057cbf49 NB |
424 | { |
425 | struct tcm_vhost_nacl *nacl; | |
426 | ||
427 | nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL); | |
428 | if (!nacl) { | |
744627e9 | 429 | pr_err("Unable to allocate struct tcm_vhost_nacl\n"); |
057cbf49 NB |
430 | return NULL; |
431 | } | |
432 | ||
433 | return &nacl->se_node_acl; | |
434 | } | |
435 | ||
683bd967 AH |
436 | static void |
437 | tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg, | |
438 | struct se_node_acl *se_nacl) | |
057cbf49 NB |
439 | { |
440 | struct tcm_vhost_nacl *nacl = container_of(se_nacl, | |
441 | struct tcm_vhost_nacl, se_node_acl); | |
442 | kfree(nacl); | |
443 | } | |
444 | ||
445 | static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg) | |
446 | { | |
447 | return 1; | |
448 | } | |
449 | ||
450 | static void tcm_vhost_release_cmd(struct se_cmd *se_cmd) | |
451 | { | |
452 | return; | |
453 | } | |
454 | ||
455 | static int tcm_vhost_shutdown_session(struct se_session *se_sess) | |
456 | { | |
457 | return 0; | |
458 | } | |
459 | ||
460 | static void tcm_vhost_close_session(struct se_session *se_sess) | |
461 | { | |
462 | return; | |
463 | } | |
464 | ||
465 | static u32 tcm_vhost_sess_get_index(struct se_session *se_sess) | |
466 | { | |
467 | return 0; | |
468 | } | |
469 | ||
470 | static int tcm_vhost_write_pending(struct se_cmd *se_cmd) | |
471 | { | |
472 | /* Go ahead and process the write immediately */ | |
473 | target_execute_cmd(se_cmd); | |
474 | return 0; | |
475 | } | |
476 | ||
477 | static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd) | |
478 | { | |
479 | return 0; | |
480 | } | |
481 | ||
482 | static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl) | |
483 | { | |
484 | return; | |
485 | } | |
486 | ||
487 | static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd) | |
488 | { | |
489 | return 0; | |
490 | } | |
491 | ||
492 | static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd) | |
493 | { | |
494 | return 0; | |
495 | } | |
496 | ||
101998f6 NB |
497 | static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd) |
498 | { | |
499 | struct vhost_scsi *vs = tv_cmd->tvc_vhost; | |
500 | ||
9d6064a3 | 501 | llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list); |
101998f6 NB |
502 | |
503 | vhost_work_queue(&vs->dev, &vs->vs_completion_work); | |
504 | } | |
057cbf49 NB |
505 | |
506 | static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd) | |
507 | { | |
508 | struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, | |
509 | struct tcm_vhost_cmd, tvc_se_cmd); | |
510 | vhost_scsi_complete_cmd(tv_cmd); | |
511 | return 0; | |
512 | } | |
513 | ||
514 | static int tcm_vhost_queue_status(struct se_cmd *se_cmd) | |
515 | { | |
516 | struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, | |
517 | struct tcm_vhost_cmd, tvc_se_cmd); | |
518 | vhost_scsi_complete_cmd(tv_cmd); | |
519 | return 0; | |
520 | } | |
521 | ||
522 | static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd) | |
523 | { | |
524 | return 0; | |
525 | } | |
526 | ||
a6c9af87 AH |
527 | static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt) |
528 | { | |
529 | vs->vs_events_nr--; | |
530 | kfree(evt); | |
531 | } | |
532 | ||
683bd967 AH |
533 | static struct tcm_vhost_evt * |
534 | tcm_vhost_allocate_evt(struct vhost_scsi *vs, | |
535 | u32 event, u32 reason) | |
a6c9af87 | 536 | { |
3ab2e420 | 537 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
a6c9af87 AH |
538 | struct tcm_vhost_evt *evt; |
539 | ||
540 | if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) { | |
541 | vs->vs_events_missed = true; | |
542 | return NULL; | |
543 | } | |
544 | ||
545 | evt = kzalloc(sizeof(*evt), GFP_KERNEL); | |
546 | if (!evt) { | |
547 | vq_err(vq, "Failed to allocate tcm_vhost_evt\n"); | |
548 | vs->vs_events_missed = true; | |
549 | return NULL; | |
550 | } | |
551 | ||
552 | evt->event.event = event; | |
553 | evt->event.reason = reason; | |
554 | vs->vs_events_nr++; | |
555 | ||
556 | return evt; | |
557 | } | |
558 | ||
057cbf49 NB |
559 | static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd) |
560 | { | |
561 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; | |
562 | ||
563 | /* TODO locking against target/backend threads? */ | |
564 | transport_generic_free_cmd(se_cmd, 1); | |
565 | ||
566 | if (tv_cmd->tvc_sgl_count) { | |
567 | u32 i; | |
568 | for (i = 0; i < tv_cmd->tvc_sgl_count; i++) | |
569 | put_page(sg_page(&tv_cmd->tvc_sgl[i])); | |
570 | ||
571 | kfree(tv_cmd->tvc_sgl); | |
572 | } | |
573 | ||
f2f0173d AH |
574 | tcm_vhost_put_inflight(tv_cmd->inflight); |
575 | ||
057cbf49 NB |
576 | kfree(tv_cmd); |
577 | } | |
578 | ||
683bd967 AH |
579 | static void |
580 | tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt) | |
a6c9af87 | 581 | { |
3ab2e420 | 582 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
a6c9af87 AH |
583 | struct virtio_scsi_event *event = &evt->event; |
584 | struct virtio_scsi_event __user *eventp; | |
585 | unsigned out, in; | |
586 | int head, ret; | |
587 | ||
588 | if (!vq->private_data) { | |
589 | vs->vs_events_missed = true; | |
590 | return; | |
591 | } | |
592 | ||
593 | again: | |
594 | vhost_disable_notify(&vs->dev, vq); | |
595 | head = vhost_get_vq_desc(&vs->dev, vq, vq->iov, | |
596 | ARRAY_SIZE(vq->iov), &out, &in, | |
597 | NULL, NULL); | |
598 | if (head < 0) { | |
599 | vs->vs_events_missed = true; | |
600 | return; | |
601 | } | |
602 | if (head == vq->num) { | |
603 | if (vhost_enable_notify(&vs->dev, vq)) | |
604 | goto again; | |
605 | vs->vs_events_missed = true; | |
606 | return; | |
607 | } | |
608 | ||
609 | if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) { | |
610 | vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n", | |
611 | vq->iov[out].iov_len); | |
612 | vs->vs_events_missed = true; | |
613 | return; | |
614 | } | |
615 | ||
616 | if (vs->vs_events_missed) { | |
617 | event->event |= VIRTIO_SCSI_T_EVENTS_MISSED; | |
618 | vs->vs_events_missed = false; | |
619 | } | |
620 | ||
621 | eventp = vq->iov[out].iov_base; | |
622 | ret = __copy_to_user(eventp, event, sizeof(*event)); | |
623 | if (!ret) | |
624 | vhost_add_used_and_signal(&vs->dev, vq, head, 0); | |
625 | else | |
626 | vq_err(vq, "Faulted on tcm_vhost_send_event\n"); | |
627 | } | |
628 | ||
629 | static void tcm_vhost_evt_work(struct vhost_work *work) | |
630 | { | |
631 | struct vhost_scsi *vs = container_of(work, struct vhost_scsi, | |
632 | vs_event_work); | |
3ab2e420 | 633 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
a6c9af87 AH |
634 | struct tcm_vhost_evt *evt; |
635 | struct llist_node *llnode; | |
636 | ||
637 | mutex_lock(&vq->mutex); | |
638 | llnode = llist_del_all(&vs->vs_event_list); | |
639 | while (llnode) { | |
640 | evt = llist_entry(llnode, struct tcm_vhost_evt, list); | |
641 | llnode = llist_next(llnode); | |
642 | tcm_vhost_do_evt_work(vs, evt); | |
643 | tcm_vhost_free_evt(vs, evt); | |
644 | } | |
645 | mutex_unlock(&vq->mutex); | |
646 | } | |
647 | ||
057cbf49 NB |
648 | /* Fill in status and signal that we are done processing this command |
649 | * | |
650 | * This is scheduled in the vhost work queue so we are called with the owner | |
651 | * process mm and can access the vring. | |
652 | */ | |
653 | static void vhost_scsi_complete_cmd_work(struct vhost_work *work) | |
654 | { | |
655 | struct vhost_scsi *vs = container_of(work, struct vhost_scsi, | |
656 | vs_completion_work); | |
1b7f390e | 657 | DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ); |
9d6064a3 | 658 | struct virtio_scsi_cmd_resp v_rsp; |
057cbf49 | 659 | struct tcm_vhost_cmd *tv_cmd; |
9d6064a3 AH |
660 | struct llist_node *llnode; |
661 | struct se_cmd *se_cmd; | |
1b7f390e | 662 | int ret, vq; |
057cbf49 | 663 | |
1b7f390e | 664 | bitmap_zero(signal, VHOST_SCSI_MAX_VQ); |
9d6064a3 AH |
665 | llnode = llist_del_all(&vs->vs_completion_list); |
666 | while (llnode) { | |
667 | tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd, | |
668 | tvc_completion_list); | |
669 | llnode = llist_next(llnode); | |
670 | se_cmd = &tv_cmd->tvc_se_cmd; | |
057cbf49 NB |
671 | |
672 | pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__, | |
673 | tv_cmd, se_cmd->residual_count, se_cmd->scsi_status); | |
674 | ||
675 | memset(&v_rsp, 0, sizeof(v_rsp)); | |
676 | v_rsp.resid = se_cmd->residual_count; | |
677 | /* TODO is status_qualifier field needed? */ | |
678 | v_rsp.status = se_cmd->scsi_status; | |
679 | v_rsp.sense_len = se_cmd->scsi_sense_length; | |
680 | memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf, | |
681 | v_rsp.sense_len); | |
682 | ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp)); | |
1b7f390e | 683 | if (likely(ret == 0)) { |
3ab2e420 | 684 | struct vhost_scsi_virtqueue *q; |
1b7f390e | 685 | vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0); |
3ab2e420 AH |
686 | q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq); |
687 | vq = q - vs->vqs; | |
1b7f390e AH |
688 | __set_bit(vq, signal); |
689 | } else | |
057cbf49 NB |
690 | pr_err("Faulted on virtio_scsi_cmd_resp\n"); |
691 | ||
692 | vhost_scsi_free_cmd(tv_cmd); | |
693 | } | |
694 | ||
1b7f390e AH |
695 | vq = -1; |
696 | while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1)) | |
697 | < VHOST_SCSI_MAX_VQ) | |
3ab2e420 | 698 | vhost_signal(&vs->dev, &vs->vqs[vq].vq); |
057cbf49 NB |
699 | } |
700 | ||
683bd967 AH |
701 | static struct tcm_vhost_cmd * |
702 | vhost_scsi_allocate_cmd(struct vhost_virtqueue *vq, | |
98718312 | 703 | struct tcm_vhost_tpg *tpg, |
683bd967 AH |
704 | struct virtio_scsi_cmd_req *v_req, |
705 | u32 exp_data_len, | |
706 | int data_direction) | |
057cbf49 NB |
707 | { |
708 | struct tcm_vhost_cmd *tv_cmd; | |
709 | struct tcm_vhost_nexus *tv_nexus; | |
057cbf49 | 710 | |
98718312 | 711 | tv_nexus = tpg->tpg_nexus; |
057cbf49 NB |
712 | if (!tv_nexus) { |
713 | pr_err("Unable to locate active struct tcm_vhost_nexus\n"); | |
714 | return ERR_PTR(-EIO); | |
715 | } | |
057cbf49 NB |
716 | |
717 | tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC); | |
718 | if (!tv_cmd) { | |
719 | pr_err("Unable to allocate struct tcm_vhost_cmd\n"); | |
720 | return ERR_PTR(-ENOMEM); | |
721 | } | |
057cbf49 | 722 | tv_cmd->tvc_tag = v_req->tag; |
9f0abc15 NB |
723 | tv_cmd->tvc_task_attr = v_req->task_attr; |
724 | tv_cmd->tvc_exp_data_len = exp_data_len; | |
725 | tv_cmd->tvc_data_direction = data_direction; | |
726 | tv_cmd->tvc_nexus = tv_nexus; | |
f2f0173d | 727 | tv_cmd->inflight = tcm_vhost_get_inflight(vq); |
057cbf49 | 728 | |
057cbf49 NB |
729 | return tv_cmd; |
730 | } | |
731 | ||
732 | /* | |
733 | * Map a user memory range into a scatterlist | |
734 | * | |
735 | * Returns the number of scatterlist entries used or -errno on error. | |
736 | */ | |
683bd967 AH |
737 | static int |
738 | vhost_scsi_map_to_sgl(struct scatterlist *sgl, | |
739 | unsigned int sgl_count, | |
740 | struct iovec *iov, | |
741 | int write) | |
057cbf49 | 742 | { |
1810053e | 743 | unsigned int npages = 0, pages_nr, offset, nbytes; |
057cbf49 | 744 | struct scatterlist *sg = sgl; |
1810053e AH |
745 | void __user *ptr = iov->iov_base; |
746 | size_t len = iov->iov_len; | |
747 | struct page **pages; | |
748 | int ret, i; | |
057cbf49 | 749 | |
1810053e AH |
750 | pages_nr = iov_num_pages(iov); |
751 | if (pages_nr > sgl_count) | |
752 | return -ENOBUFS; | |
057cbf49 | 753 | |
1810053e AH |
754 | pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL); |
755 | if (!pages) | |
756 | return -ENOMEM; | |
057cbf49 | 757 | |
1810053e AH |
758 | ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages); |
759 | /* No pages were pinned */ | |
760 | if (ret < 0) | |
761 | goto out; | |
762 | /* Less pages pinned than wanted */ | |
763 | if (ret != pages_nr) { | |
764 | for (i = 0; i < ret; i++) | |
765 | put_page(pages[i]); | |
766 | ret = -EFAULT; | |
767 | goto out; | |
768 | } | |
769 | ||
770 | while (len > 0) { | |
771 | offset = (uintptr_t)ptr & ~PAGE_MASK; | |
772 | nbytes = min_t(unsigned int, PAGE_SIZE - offset, len); | |
773 | sg_set_page(sg, pages[npages], nbytes, offset); | |
057cbf49 NB |
774 | ptr += nbytes; |
775 | len -= nbytes; | |
776 | sg++; | |
777 | npages++; | |
778 | } | |
057cbf49 | 779 | |
1810053e AH |
780 | out: |
781 | kfree(pages); | |
057cbf49 NB |
782 | return ret; |
783 | } | |
784 | ||
683bd967 AH |
785 | static int |
786 | vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd, | |
787 | struct iovec *iov, | |
788 | unsigned int niov, | |
789 | int write) | |
057cbf49 NB |
790 | { |
791 | int ret; | |
792 | unsigned int i; | |
793 | u32 sgl_count; | |
794 | struct scatterlist *sg; | |
795 | ||
796 | /* | |
797 | * Find out how long sglist needs to be | |
798 | */ | |
799 | sgl_count = 0; | |
f3158f36 AH |
800 | for (i = 0; i < niov; i++) |
801 | sgl_count += iov_num_pages(&iov[i]); | |
802 | ||
057cbf49 NB |
803 | /* TODO overflow checking */ |
804 | ||
805 | sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC); | |
806 | if (!sg) | |
807 | return -ENOMEM; | |
f0e0e9bb FW |
808 | pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__, |
809 | sg, sgl_count, !sg); | |
057cbf49 NB |
810 | sg_init_table(sg, sgl_count); |
811 | ||
812 | tv_cmd->tvc_sgl = sg; | |
813 | tv_cmd->tvc_sgl_count = sgl_count; | |
814 | ||
815 | pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count); | |
816 | for (i = 0; i < niov; i++) { | |
1810053e | 817 | ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write); |
057cbf49 NB |
818 | if (ret < 0) { |
819 | for (i = 0; i < tv_cmd->tvc_sgl_count; i++) | |
820 | put_page(sg_page(&tv_cmd->tvc_sgl[i])); | |
821 | kfree(tv_cmd->tvc_sgl); | |
822 | tv_cmd->tvc_sgl = NULL; | |
823 | tv_cmd->tvc_sgl_count = 0; | |
824 | return ret; | |
825 | } | |
826 | ||
827 | sg += ret; | |
828 | sgl_count -= ret; | |
829 | } | |
830 | return 0; | |
831 | } | |
832 | ||
833 | static void tcm_vhost_submission_work(struct work_struct *work) | |
834 | { | |
835 | struct tcm_vhost_cmd *tv_cmd = | |
836 | container_of(work, struct tcm_vhost_cmd, work); | |
9f0abc15 | 837 | struct tcm_vhost_nexus *tv_nexus; |
057cbf49 NB |
838 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; |
839 | struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL; | |
840 | int rc, sg_no_bidi = 0; | |
057cbf49 NB |
841 | |
842 | if (tv_cmd->tvc_sgl_count) { | |
843 | sg_ptr = tv_cmd->tvc_sgl; | |
057cbf49 NB |
844 | /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */ |
845 | #if 0 | |
846 | if (se_cmd->se_cmd_flags & SCF_BIDI) { | |
847 | sg_bidi_ptr = NULL; | |
848 | sg_no_bidi = 0; | |
849 | } | |
850 | #endif | |
851 | } else { | |
852 | sg_ptr = NULL; | |
853 | } | |
9f0abc15 NB |
854 | tv_nexus = tv_cmd->tvc_nexus; |
855 | ||
856 | rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess, | |
857 | tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0], | |
858 | tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len, | |
859 | tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction, | |
860 | 0, sg_ptr, tv_cmd->tvc_sgl_count, | |
861 | sg_bidi_ptr, sg_no_bidi); | |
057cbf49 NB |
862 | if (rc < 0) { |
863 | transport_send_check_condition_and_sense(se_cmd, | |
9f0abc15 | 864 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); |
057cbf49 | 865 | transport_generic_free_cmd(se_cmd, 0); |
057cbf49 | 866 | } |
057cbf49 NB |
867 | } |
868 | ||
683bd967 AH |
869 | static void |
870 | vhost_scsi_send_bad_target(struct vhost_scsi *vs, | |
871 | struct vhost_virtqueue *vq, | |
872 | int head, unsigned out) | |
637ab21e AH |
873 | { |
874 | struct virtio_scsi_cmd_resp __user *resp; | |
875 | struct virtio_scsi_cmd_resp rsp; | |
876 | int ret; | |
877 | ||
878 | memset(&rsp, 0, sizeof(rsp)); | |
879 | rsp.response = VIRTIO_SCSI_S_BAD_TARGET; | |
880 | resp = vq->iov[out].iov_base; | |
881 | ret = __copy_to_user(resp, &rsp, sizeof(rsp)); | |
882 | if (!ret) | |
883 | vhost_add_used_and_signal(&vs->dev, vq, head, 0); | |
884 | else | |
885 | pr_err("Faulted on virtio_scsi_cmd_resp\n"); | |
886 | } | |
887 | ||
683bd967 AH |
888 | static void |
889 | vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) | |
057cbf49 | 890 | { |
4f7f46d3 | 891 | struct tcm_vhost_tpg **vs_tpg; |
057cbf49 | 892 | struct virtio_scsi_cmd_req v_req; |
98718312 | 893 | struct tcm_vhost_tpg *tpg; |
057cbf49 NB |
894 | struct tcm_vhost_cmd *tv_cmd; |
895 | u32 exp_data_len, data_first, data_num, data_direction; | |
896 | unsigned out, in, i; | |
897 | int head, ret; | |
67e18cf9 | 898 | u8 target; |
057cbf49 | 899 | |
4f7f46d3 AH |
900 | /* |
901 | * We can handle the vq only after the endpoint is setup by calling the | |
902 | * VHOST_SCSI_SET_ENDPOINT ioctl. | |
903 | * | |
904 | * TODO: Check that we are running from vhost_worker which acts | |
905 | * as read-side critical section for vhost kind of RCU. | |
906 | * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h | |
907 | */ | |
908 | vs_tpg = rcu_dereference_check(vq->private_data, 1); | |
909 | if (!vs_tpg) | |
057cbf49 | 910 | return; |
057cbf49 NB |
911 | |
912 | mutex_lock(&vq->mutex); | |
913 | vhost_disable_notify(&vs->dev, vq); | |
914 | ||
915 | for (;;) { | |
916 | head = vhost_get_vq_desc(&vs->dev, vq, vq->iov, | |
917 | ARRAY_SIZE(vq->iov), &out, &in, | |
918 | NULL, NULL); | |
919 | pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", | |
920 | head, out, in); | |
921 | /* On error, stop handling until the next kick. */ | |
922 | if (unlikely(head < 0)) | |
923 | break; | |
924 | /* Nothing new? Wait for eventfd to tell us they refilled. */ | |
925 | if (head == vq->num) { | |
926 | if (unlikely(vhost_enable_notify(&vs->dev, vq))) { | |
927 | vhost_disable_notify(&vs->dev, vq); | |
928 | continue; | |
929 | } | |
930 | break; | |
931 | } | |
932 | ||
933 | /* FIXME: BIDI operation */ | |
934 | if (out == 1 && in == 1) { | |
935 | data_direction = DMA_NONE; | |
936 | data_first = 0; | |
937 | data_num = 0; | |
938 | } else if (out == 1 && in > 1) { | |
939 | data_direction = DMA_FROM_DEVICE; | |
940 | data_first = out + 1; | |
941 | data_num = in - 1; | |
942 | } else if (out > 1 && in == 1) { | |
943 | data_direction = DMA_TO_DEVICE; | |
944 | data_first = 1; | |
945 | data_num = out - 1; | |
946 | } else { | |
947 | vq_err(vq, "Invalid buffer layout out: %u in: %u\n", | |
948 | out, in); | |
949 | break; | |
950 | } | |
951 | ||
952 | /* | |
953 | * Check for a sane resp buffer so we can report errors to | |
954 | * the guest. | |
955 | */ | |
956 | if (unlikely(vq->iov[out].iov_len != | |
957 | sizeof(struct virtio_scsi_cmd_resp))) { | |
958 | vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu" | |
959 | " bytes\n", vq->iov[out].iov_len); | |
960 | break; | |
961 | } | |
962 | ||
963 | if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) { | |
964 | vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu" | |
965 | " bytes\n", vq->iov[0].iov_len); | |
966 | break; | |
967 | } | |
968 | pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p," | |
969 | " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req)); | |
970 | ret = __copy_from_user(&v_req, vq->iov[0].iov_base, | |
971 | sizeof(v_req)); | |
972 | if (unlikely(ret)) { | |
973 | vq_err(vq, "Faulted on virtio_scsi_cmd_req\n"); | |
974 | break; | |
975 | } | |
976 | ||
67e18cf9 AH |
977 | /* Extract the tpgt */ |
978 | target = v_req.lun[1]; | |
98718312 | 979 | tpg = ACCESS_ONCE(vs_tpg[target]); |
67e18cf9 AH |
980 | |
981 | /* Target does not exist, fail the request */ | |
98718312 | 982 | if (unlikely(!tpg)) { |
637ab21e | 983 | vhost_scsi_send_bad_target(vs, vq, head, out); |
67e18cf9 AH |
984 | continue; |
985 | } | |
986 | ||
057cbf49 NB |
987 | exp_data_len = 0; |
988 | for (i = 0; i < data_num; i++) | |
989 | exp_data_len += vq->iov[data_first + i].iov_len; | |
990 | ||
98718312 | 991 | tv_cmd = vhost_scsi_allocate_cmd(vq, tpg, &v_req, |
057cbf49 NB |
992 | exp_data_len, data_direction); |
993 | if (IS_ERR(tv_cmd)) { | |
994 | vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n", | |
995 | PTR_ERR(tv_cmd)); | |
055f648c | 996 | goto err_cmd; |
057cbf49 NB |
997 | } |
998 | pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction" | |
999 | ": %d\n", tv_cmd, exp_data_len, data_direction); | |
1000 | ||
1001 | tv_cmd->tvc_vhost = vs; | |
1b7f390e | 1002 | tv_cmd->tvc_vq = vq; |
057cbf49 NB |
1003 | tv_cmd->tvc_resp = vq->iov[out].iov_base; |
1004 | ||
1005 | /* | |
1006 | * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb | |
1007 | * that will be used by tcm_vhost_new_cmd_map() and down into | |
1008 | * target_setup_cmd_from_cdb() | |
1009 | */ | |
1010 | memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE); | |
1011 | /* | |
1012 | * Check that the recieved CDB size does not exceeded our | |
1013 | * hardcoded max for tcm_vhost | |
1014 | */ | |
1015 | /* TODO what if cdb was too small for varlen cdb header? */ | |
1016 | if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) > | |
1017 | TCM_VHOST_MAX_CDB_SIZE)) { | |
1018 | vq_err(vq, "Received SCSI CDB with command_size: %d that" | |
1019 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", | |
1020 | scsi_command_size(tv_cmd->tvc_cdb), | |
1021 | TCM_VHOST_MAX_CDB_SIZE); | |
055f648c | 1022 | goto err_free; |
057cbf49 NB |
1023 | } |
1024 | tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF; | |
1025 | ||
1026 | pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", | |
1027 | tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun); | |
1028 | ||
1029 | if (data_direction != DMA_NONE) { | |
1030 | ret = vhost_scsi_map_iov_to_sgl(tv_cmd, | |
1031 | &vq->iov[data_first], data_num, | |
1032 | data_direction == DMA_TO_DEVICE); | |
1033 | if (unlikely(ret)) { | |
1034 | vq_err(vq, "Failed to map iov to sgl\n"); | |
055f648c | 1035 | goto err_free; |
057cbf49 NB |
1036 | } |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * Save the descriptor from vhost_get_vq_desc() to be used to | |
1041 | * complete the virtio-scsi request in TCM callback context via | |
1042 | * tcm_vhost_queue_data_in() and tcm_vhost_queue_status() | |
1043 | */ | |
1044 | tv_cmd->tvc_vq_desc = head; | |
1045 | /* | |
1046 | * Dispatch tv_cmd descriptor for cmwq execution in process | |
1047 | * context provided by tcm_vhost_workqueue. This also ensures | |
1048 | * tv_cmd is executed on the same kworker CPU as this vhost | |
1049 | * thread to gain positive L2 cache locality effects.. | |
1050 | */ | |
1051 | INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work); | |
1052 | queue_work(tcm_vhost_workqueue, &tv_cmd->work); | |
1053 | } | |
1054 | ||
1055 | mutex_unlock(&vq->mutex); | |
7ea206cf AH |
1056 | return; |
1057 | ||
055f648c | 1058 | err_free: |
7ea206cf | 1059 | vhost_scsi_free_cmd(tv_cmd); |
055f648c AH |
1060 | err_cmd: |
1061 | vhost_scsi_send_bad_target(vs, vq, head, out); | |
7ea206cf | 1062 | mutex_unlock(&vq->mutex); |
057cbf49 NB |
1063 | } |
1064 | ||
1065 | static void vhost_scsi_ctl_handle_kick(struct vhost_work *work) | |
1066 | { | |
101998f6 | 1067 | pr_debug("%s: The handling func for control queue.\n", __func__); |
057cbf49 NB |
1068 | } |
1069 | ||
683bd967 AH |
1070 | static void |
1071 | tcm_vhost_send_evt(struct vhost_scsi *vs, | |
1072 | struct tcm_vhost_tpg *tpg, | |
1073 | struct se_lun *lun, | |
1074 | u32 event, | |
1075 | u32 reason) | |
a6c9af87 AH |
1076 | { |
1077 | struct tcm_vhost_evt *evt; | |
1078 | ||
1079 | evt = tcm_vhost_allocate_evt(vs, event, reason); | |
1080 | if (!evt) | |
1081 | return; | |
1082 | ||
1083 | if (tpg && lun) { | |
1084 | /* TODO: share lun setup code with virtio-scsi.ko */ | |
1085 | /* | |
1086 | * Note: evt->event is zeroed when we allocate it and | |
1087 | * lun[4-7] need to be zero according to virtio-scsi spec. | |
1088 | */ | |
1089 | evt->event.lun[0] = 0x01; | |
1090 | evt->event.lun[1] = tpg->tport_tpgt & 0xFF; | |
1091 | if (lun->unpacked_lun >= 256) | |
1092 | evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ; | |
1093 | evt->event.lun[3] = lun->unpacked_lun & 0xFF; | |
1094 | } | |
1095 | ||
1096 | llist_add(&evt->list, &vs->vs_event_list); | |
1097 | vhost_work_queue(&vs->dev, &vs->vs_event_work); | |
1098 | } | |
1099 | ||
057cbf49 NB |
1100 | static void vhost_scsi_evt_handle_kick(struct vhost_work *work) |
1101 | { | |
a6c9af87 AH |
1102 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
1103 | poll.work); | |
1104 | struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); | |
1105 | ||
1106 | mutex_lock(&vq->mutex); | |
1107 | if (!vq->private_data) | |
1108 | goto out; | |
1109 | ||
1110 | if (vs->vs_events_missed) | |
1111 | tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); | |
1112 | out: | |
1113 | mutex_unlock(&vq->mutex); | |
057cbf49 NB |
1114 | } |
1115 | ||
1116 | static void vhost_scsi_handle_kick(struct vhost_work *work) | |
1117 | { | |
1118 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, | |
1119 | poll.work); | |
1120 | struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); | |
1121 | ||
1b7f390e | 1122 | vhost_scsi_handle_vq(vs, vq); |
057cbf49 NB |
1123 | } |
1124 | ||
4f7f46d3 AH |
1125 | static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index) |
1126 | { | |
3ab2e420 | 1127 | vhost_poll_flush(&vs->vqs[index].vq.poll); |
4f7f46d3 AH |
1128 | } |
1129 | ||
3dfbff32 | 1130 | /* Callers must hold dev mutex */ |
4f7f46d3 AH |
1131 | static void vhost_scsi_flush(struct vhost_scsi *vs) |
1132 | { | |
f2f0173d | 1133 | struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ]; |
4f7f46d3 AH |
1134 | int i; |
1135 | ||
f2f0173d AH |
1136 | /* Init new inflight and remember the old inflight */ |
1137 | tcm_vhost_init_inflight(vs, old_inflight); | |
1138 | ||
1139 | /* | |
1140 | * The inflight->kref was initialized to 1. We decrement it here to | |
1141 | * indicate the start of the flush operation so that it will reach 0 | |
1142 | * when all the reqs are finished. | |
1143 | */ | |
1144 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) | |
1145 | kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight); | |
1146 | ||
1147 | /* Flush both the vhost poll and vhost work */ | |
4f7f46d3 AH |
1148 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) |
1149 | vhost_scsi_flush_vq(vs, i); | |
1150 | vhost_work_flush(&vs->dev, &vs->vs_completion_work); | |
a6c9af87 | 1151 | vhost_work_flush(&vs->dev, &vs->vs_event_work); |
f2f0173d AH |
1152 | |
1153 | /* Wait for all reqs issued before the flush to be finished */ | |
1154 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) | |
1155 | wait_for_completion(&old_inflight[i]->comp); | |
4f7f46d3 AH |
1156 | } |
1157 | ||
057cbf49 NB |
1158 | /* |
1159 | * Called from vhost_scsi_ioctl() context to walk the list of available | |
1160 | * tcm_vhost_tpg with an active struct tcm_vhost_nexus | |
f2b7daf5 AH |
1161 | * |
1162 | * The lock nesting rule is: | |
1163 | * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex | |
057cbf49 | 1164 | */ |
683bd967 AH |
1165 | static int |
1166 | vhost_scsi_set_endpoint(struct vhost_scsi *vs, | |
1167 | struct vhost_scsi_target *t) | |
057cbf49 NB |
1168 | { |
1169 | struct tcm_vhost_tport *tv_tport; | |
98718312 | 1170 | struct tcm_vhost_tpg *tpg; |
4f7f46d3 AH |
1171 | struct tcm_vhost_tpg **vs_tpg; |
1172 | struct vhost_virtqueue *vq; | |
1173 | int index, ret, i, len; | |
67e18cf9 | 1174 | bool match = false; |
057cbf49 | 1175 | |
f2b7daf5 | 1176 | mutex_lock(&tcm_vhost_mutex); |
057cbf49 | 1177 | mutex_lock(&vs->dev.mutex); |
f2b7daf5 | 1178 | |
057cbf49 NB |
1179 | /* Verify that ring has been setup correctly. */ |
1180 | for (index = 0; index < vs->dev.nvqs; ++index) { | |
1181 | /* Verify that ring has been setup correctly. */ | |
3ab2e420 | 1182 | if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { |
f2b7daf5 AH |
1183 | ret = -EFAULT; |
1184 | goto out; | |
057cbf49 NB |
1185 | } |
1186 | } | |
057cbf49 | 1187 | |
4f7f46d3 AH |
1188 | len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET; |
1189 | vs_tpg = kzalloc(len, GFP_KERNEL); | |
1190 | if (!vs_tpg) { | |
f2b7daf5 AH |
1191 | ret = -ENOMEM; |
1192 | goto out; | |
4f7f46d3 AH |
1193 | } |
1194 | if (vs->vs_tpg) | |
1195 | memcpy(vs_tpg, vs->vs_tpg, len); | |
1196 | ||
98718312 AH |
1197 | list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) { |
1198 | mutex_lock(&tpg->tv_tpg_mutex); | |
1199 | if (!tpg->tpg_nexus) { | |
1200 | mutex_unlock(&tpg->tv_tpg_mutex); | |
057cbf49 NB |
1201 | continue; |
1202 | } | |
98718312 AH |
1203 | if (tpg->tv_tpg_vhost_count != 0) { |
1204 | mutex_unlock(&tpg->tv_tpg_mutex); | |
057cbf49 NB |
1205 | continue; |
1206 | } | |
98718312 | 1207 | tv_tport = tpg->tport; |
057cbf49 | 1208 | |
67e18cf9 | 1209 | if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) { |
98718312 | 1210 | if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) { |
4f7f46d3 | 1211 | kfree(vs_tpg); |
98718312 | 1212 | mutex_unlock(&tpg->tv_tpg_mutex); |
f2b7daf5 AH |
1213 | ret = -EEXIST; |
1214 | goto out; | |
101998f6 | 1215 | } |
98718312 AH |
1216 | tpg->tv_tpg_vhost_count++; |
1217 | tpg->vhost_scsi = vs; | |
1218 | vs_tpg[tpg->tport_tpgt] = tpg; | |
057cbf49 | 1219 | smp_mb__after_atomic_inc(); |
67e18cf9 | 1220 | match = true; |
057cbf49 | 1221 | } |
98718312 | 1222 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 | 1223 | } |
67e18cf9 AH |
1224 | |
1225 | if (match) { | |
1226 | memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn, | |
1227 | sizeof(vs->vs_vhost_wwpn)); | |
4f7f46d3 | 1228 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { |
3ab2e420 | 1229 | vq = &vs->vqs[i].vq; |
4f7f46d3 AH |
1230 | /* Flushing the vhost_work acts as synchronize_rcu */ |
1231 | mutex_lock(&vq->mutex); | |
1232 | rcu_assign_pointer(vq->private_data, vs_tpg); | |
dfd5d569 | 1233 | vhost_init_used(vq); |
4f7f46d3 AH |
1234 | mutex_unlock(&vq->mutex); |
1235 | } | |
67e18cf9 AH |
1236 | ret = 0; |
1237 | } else { | |
1238 | ret = -EEXIST; | |
1239 | } | |
1240 | ||
4f7f46d3 AH |
1241 | /* |
1242 | * Act as synchronize_rcu to make sure access to | |
1243 | * old vs->vs_tpg is finished. | |
1244 | */ | |
1245 | vhost_scsi_flush(vs); | |
1246 | kfree(vs->vs_tpg); | |
1247 | vs->vs_tpg = vs_tpg; | |
1248 | ||
f2b7daf5 | 1249 | out: |
67e18cf9 | 1250 | mutex_unlock(&vs->dev.mutex); |
f2b7daf5 | 1251 | mutex_unlock(&tcm_vhost_mutex); |
67e18cf9 | 1252 | return ret; |
057cbf49 NB |
1253 | } |
1254 | ||
683bd967 AH |
1255 | static int |
1256 | vhost_scsi_clear_endpoint(struct vhost_scsi *vs, | |
1257 | struct vhost_scsi_target *t) | |
057cbf49 NB |
1258 | { |
1259 | struct tcm_vhost_tport *tv_tport; | |
98718312 | 1260 | struct tcm_vhost_tpg *tpg; |
4f7f46d3 AH |
1261 | struct vhost_virtqueue *vq; |
1262 | bool match = false; | |
67e18cf9 AH |
1263 | int index, ret, i; |
1264 | u8 target; | |
057cbf49 | 1265 | |
f2b7daf5 | 1266 | mutex_lock(&tcm_vhost_mutex); |
057cbf49 NB |
1267 | mutex_lock(&vs->dev.mutex); |
1268 | /* Verify that ring has been setup correctly. */ | |
1269 | for (index = 0; index < vs->dev.nvqs; ++index) { | |
3ab2e420 | 1270 | if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { |
101998f6 | 1271 | ret = -EFAULT; |
038e0af4 | 1272 | goto err_dev; |
057cbf49 NB |
1273 | } |
1274 | } | |
4f7f46d3 AH |
1275 | |
1276 | if (!vs->vs_tpg) { | |
f2b7daf5 AH |
1277 | ret = 0; |
1278 | goto err_dev; | |
4f7f46d3 AH |
1279 | } |
1280 | ||
67e18cf9 AH |
1281 | for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { |
1282 | target = i; | |
98718312 AH |
1283 | tpg = vs->vs_tpg[target]; |
1284 | if (!tpg) | |
67e18cf9 AH |
1285 | continue; |
1286 | ||
98718312 AH |
1287 | mutex_lock(&tpg->tv_tpg_mutex); |
1288 | tv_tport = tpg->tport; | |
67e18cf9 AH |
1289 | if (!tv_tport) { |
1290 | ret = -ENODEV; | |
038e0af4 | 1291 | goto err_tpg; |
67e18cf9 AH |
1292 | } |
1293 | ||
1294 | if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) { | |
98718312 | 1295 | pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu" |
67e18cf9 | 1296 | " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n", |
98718312 | 1297 | tv_tport->tport_name, tpg->tport_tpgt, |
67e18cf9 AH |
1298 | t->vhost_wwpn, t->vhost_tpgt); |
1299 | ret = -EINVAL; | |
038e0af4 | 1300 | goto err_tpg; |
67e18cf9 | 1301 | } |
98718312 AH |
1302 | tpg->tv_tpg_vhost_count--; |
1303 | tpg->vhost_scsi = NULL; | |
67e18cf9 | 1304 | vs->vs_tpg[target] = NULL; |
4f7f46d3 | 1305 | match = true; |
98718312 | 1306 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 | 1307 | } |
4f7f46d3 AH |
1308 | if (match) { |
1309 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { | |
3ab2e420 | 1310 | vq = &vs->vqs[i].vq; |
4f7f46d3 AH |
1311 | /* Flushing the vhost_work acts as synchronize_rcu */ |
1312 | mutex_lock(&vq->mutex); | |
1313 | rcu_assign_pointer(vq->private_data, NULL); | |
1314 | mutex_unlock(&vq->mutex); | |
1315 | } | |
1316 | } | |
1317 | /* | |
1318 | * Act as synchronize_rcu to make sure access to | |
1319 | * old vs->vs_tpg is finished. | |
1320 | */ | |
1321 | vhost_scsi_flush(vs); | |
1322 | kfree(vs->vs_tpg); | |
1323 | vs->vs_tpg = NULL; | |
a6c9af87 | 1324 | WARN_ON(vs->vs_events_nr); |
057cbf49 | 1325 | mutex_unlock(&vs->dev.mutex); |
f2b7daf5 | 1326 | mutex_unlock(&tcm_vhost_mutex); |
057cbf49 | 1327 | return 0; |
101998f6 | 1328 | |
038e0af4 | 1329 | err_tpg: |
98718312 | 1330 | mutex_unlock(&tpg->tv_tpg_mutex); |
038e0af4 | 1331 | err_dev: |
101998f6 | 1332 | mutex_unlock(&vs->dev.mutex); |
f2b7daf5 | 1333 | mutex_unlock(&tcm_vhost_mutex); |
101998f6 | 1334 | return ret; |
057cbf49 NB |
1335 | } |
1336 | ||
4f7f46d3 AH |
1337 | static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features) |
1338 | { | |
1339 | if (features & ~VHOST_SCSI_FEATURES) | |
1340 | return -EOPNOTSUPP; | |
1341 | ||
1342 | mutex_lock(&vs->dev.mutex); | |
1343 | if ((features & (1 << VHOST_F_LOG_ALL)) && | |
1344 | !vhost_log_access_ok(&vs->dev)) { | |
1345 | mutex_unlock(&vs->dev.mutex); | |
1346 | return -EFAULT; | |
1347 | } | |
1348 | vs->dev.acked_features = features; | |
1349 | smp_wmb(); | |
1350 | vhost_scsi_flush(vs); | |
1351 | mutex_unlock(&vs->dev.mutex); | |
1352 | return 0; | |
1353 | } | |
1354 | ||
057cbf49 NB |
1355 | static int vhost_scsi_open(struct inode *inode, struct file *f) |
1356 | { | |
c7289312 | 1357 | struct vhost_scsi *vs; |
3ab2e420 | 1358 | struct vhost_virtqueue **vqs; |
1b7f390e | 1359 | int r, i; |
057cbf49 | 1360 | |
c7289312 AH |
1361 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); |
1362 | if (!vs) | |
057cbf49 NB |
1363 | return -ENOMEM; |
1364 | ||
3ab2e420 AH |
1365 | vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL); |
1366 | if (!vqs) { | |
c7289312 | 1367 | kfree(vs); |
3ab2e420 AH |
1368 | return -ENOMEM; |
1369 | } | |
1370 | ||
c7289312 AH |
1371 | vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work); |
1372 | vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work); | |
a6c9af87 | 1373 | |
c7289312 AH |
1374 | vs->vs_events_nr = 0; |
1375 | vs->vs_events_missed = false; | |
057cbf49 | 1376 | |
c7289312 AH |
1377 | vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq; |
1378 | vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; | |
1379 | vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick; | |
1380 | vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick; | |
3ab2e420 | 1381 | for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) { |
c7289312 AH |
1382 | vqs[i] = &vs->vqs[i].vq; |
1383 | vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick; | |
3ab2e420 | 1384 | } |
c7289312 | 1385 | r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ); |
f2f0173d | 1386 | |
c7289312 | 1387 | tcm_vhost_init_inflight(vs, NULL); |
f2f0173d | 1388 | |
057cbf49 | 1389 | if (r < 0) { |
3ab2e420 | 1390 | kfree(vqs); |
c7289312 | 1391 | kfree(vs); |
057cbf49 NB |
1392 | return r; |
1393 | } | |
1394 | ||
c7289312 | 1395 | f->private_data = vs; |
057cbf49 NB |
1396 | return 0; |
1397 | } | |
1398 | ||
1399 | static int vhost_scsi_release(struct inode *inode, struct file *f) | |
1400 | { | |
c7289312 | 1401 | struct vhost_scsi *vs = f->private_data; |
67e18cf9 | 1402 | struct vhost_scsi_target t; |
057cbf49 | 1403 | |
c7289312 AH |
1404 | mutex_lock(&vs->dev.mutex); |
1405 | memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn)); | |
1406 | mutex_unlock(&vs->dev.mutex); | |
1407 | vhost_scsi_clear_endpoint(vs, &t); | |
1408 | vhost_dev_stop(&vs->dev); | |
1409 | vhost_dev_cleanup(&vs->dev, false); | |
a6c9af87 | 1410 | /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */ |
c7289312 AH |
1411 | vhost_scsi_flush(vs); |
1412 | kfree(vs->dev.vqs); | |
1413 | kfree(vs); | |
057cbf49 NB |
1414 | return 0; |
1415 | } | |
1416 | ||
683bd967 AH |
1417 | static long |
1418 | vhost_scsi_ioctl(struct file *f, | |
1419 | unsigned int ioctl, | |
1420 | unsigned long arg) | |
057cbf49 NB |
1421 | { |
1422 | struct vhost_scsi *vs = f->private_data; | |
1423 | struct vhost_scsi_target backend; | |
1424 | void __user *argp = (void __user *)arg; | |
1425 | u64 __user *featurep = argp; | |
11c63418 AH |
1426 | u32 __user *eventsp = argp; |
1427 | u32 events_missed; | |
057cbf49 | 1428 | u64 features; |
101998f6 | 1429 | int r, abi_version = VHOST_SCSI_ABI_VERSION; |
3ab2e420 | 1430 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
057cbf49 NB |
1431 | |
1432 | switch (ioctl) { | |
1433 | case VHOST_SCSI_SET_ENDPOINT: | |
1434 | if (copy_from_user(&backend, argp, sizeof backend)) | |
1435 | return -EFAULT; | |
6de7145c MT |
1436 | if (backend.reserved != 0) |
1437 | return -EOPNOTSUPP; | |
057cbf49 NB |
1438 | |
1439 | return vhost_scsi_set_endpoint(vs, &backend); | |
1440 | case VHOST_SCSI_CLEAR_ENDPOINT: | |
1441 | if (copy_from_user(&backend, argp, sizeof backend)) | |
1442 | return -EFAULT; | |
6de7145c MT |
1443 | if (backend.reserved != 0) |
1444 | return -EOPNOTSUPP; | |
057cbf49 NB |
1445 | |
1446 | return vhost_scsi_clear_endpoint(vs, &backend); | |
1447 | case VHOST_SCSI_GET_ABI_VERSION: | |
101998f6 | 1448 | if (copy_to_user(argp, &abi_version, sizeof abi_version)) |
057cbf49 NB |
1449 | return -EFAULT; |
1450 | return 0; | |
11c63418 AH |
1451 | case VHOST_SCSI_SET_EVENTS_MISSED: |
1452 | if (get_user(events_missed, eventsp)) | |
1453 | return -EFAULT; | |
1454 | mutex_lock(&vq->mutex); | |
1455 | vs->vs_events_missed = events_missed; | |
1456 | mutex_unlock(&vq->mutex); | |
1457 | return 0; | |
1458 | case VHOST_SCSI_GET_EVENTS_MISSED: | |
1459 | mutex_lock(&vq->mutex); | |
1460 | events_missed = vs->vs_events_missed; | |
1461 | mutex_unlock(&vq->mutex); | |
1462 | if (put_user(events_missed, eventsp)) | |
1463 | return -EFAULT; | |
1464 | return 0; | |
057cbf49 | 1465 | case VHOST_GET_FEATURES: |
5dade710 | 1466 | features = VHOST_SCSI_FEATURES; |
057cbf49 NB |
1467 | if (copy_to_user(featurep, &features, sizeof features)) |
1468 | return -EFAULT; | |
1469 | return 0; | |
1470 | case VHOST_SET_FEATURES: | |
1471 | if (copy_from_user(&features, featurep, sizeof features)) | |
1472 | return -EFAULT; | |
1473 | return vhost_scsi_set_features(vs, features); | |
1474 | default: | |
1475 | mutex_lock(&vs->dev.mutex); | |
935cdee7 MT |
1476 | r = vhost_dev_ioctl(&vs->dev, ioctl, argp); |
1477 | /* TODO: flush backend after dev ioctl. */ | |
1478 | if (r == -ENOIOCTLCMD) | |
1479 | r = vhost_vring_ioctl(&vs->dev, ioctl, argp); | |
057cbf49 NB |
1480 | mutex_unlock(&vs->dev.mutex); |
1481 | return r; | |
1482 | } | |
1483 | } | |
1484 | ||
101998f6 NB |
1485 | #ifdef CONFIG_COMPAT |
1486 | static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl, | |
1487 | unsigned long arg) | |
1488 | { | |
1489 | return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); | |
1490 | } | |
1491 | #endif | |
1492 | ||
057cbf49 NB |
1493 | static const struct file_operations vhost_scsi_fops = { |
1494 | .owner = THIS_MODULE, | |
1495 | .release = vhost_scsi_release, | |
1496 | .unlocked_ioctl = vhost_scsi_ioctl, | |
101998f6 NB |
1497 | #ifdef CONFIG_COMPAT |
1498 | .compat_ioctl = vhost_scsi_compat_ioctl, | |
1499 | #endif | |
057cbf49 NB |
1500 | .open = vhost_scsi_open, |
1501 | .llseek = noop_llseek, | |
1502 | }; | |
1503 | ||
1504 | static struct miscdevice vhost_scsi_misc = { | |
1505 | MISC_DYNAMIC_MINOR, | |
1506 | "vhost-scsi", | |
1507 | &vhost_scsi_fops, | |
1508 | }; | |
1509 | ||
1510 | static int __init vhost_scsi_register(void) | |
1511 | { | |
1512 | return misc_register(&vhost_scsi_misc); | |
1513 | } | |
1514 | ||
1515 | static int vhost_scsi_deregister(void) | |
1516 | { | |
1517 | return misc_deregister(&vhost_scsi_misc); | |
1518 | } | |
1519 | ||
1520 | static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport) | |
1521 | { | |
1522 | switch (tport->tport_proto_id) { | |
1523 | case SCSI_PROTOCOL_SAS: | |
1524 | return "SAS"; | |
1525 | case SCSI_PROTOCOL_FCP: | |
1526 | return "FCP"; | |
1527 | case SCSI_PROTOCOL_ISCSI: | |
1528 | return "iSCSI"; | |
1529 | default: | |
1530 | break; | |
1531 | } | |
1532 | ||
1533 | return "Unknown"; | |
1534 | } | |
1535 | ||
683bd967 AH |
1536 | static void |
1537 | tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg, | |
1538 | struct se_lun *lun, bool plug) | |
a6c9af87 AH |
1539 | { |
1540 | ||
1541 | struct vhost_scsi *vs = tpg->vhost_scsi; | |
1542 | struct vhost_virtqueue *vq; | |
1543 | u32 reason; | |
1544 | ||
1545 | if (!vs) | |
1546 | return; | |
1547 | ||
1548 | mutex_lock(&vs->dev.mutex); | |
1549 | if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) { | |
1550 | mutex_unlock(&vs->dev.mutex); | |
1551 | return; | |
1552 | } | |
1553 | ||
1554 | if (plug) | |
1555 | reason = VIRTIO_SCSI_EVT_RESET_RESCAN; | |
1556 | else | |
1557 | reason = VIRTIO_SCSI_EVT_RESET_REMOVED; | |
1558 | ||
3ab2e420 | 1559 | vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
a6c9af87 AH |
1560 | mutex_lock(&vq->mutex); |
1561 | tcm_vhost_send_evt(vs, tpg, lun, | |
1562 | VIRTIO_SCSI_T_TRANSPORT_RESET, reason); | |
1563 | mutex_unlock(&vq->mutex); | |
1564 | mutex_unlock(&vs->dev.mutex); | |
1565 | } | |
1566 | ||
1567 | static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun) | |
1568 | { | |
1569 | tcm_vhost_do_plug(tpg, lun, true); | |
1570 | } | |
1571 | ||
1572 | static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun) | |
1573 | { | |
1574 | tcm_vhost_do_plug(tpg, lun, false); | |
1575 | } | |
1576 | ||
101998f6 | 1577 | static int tcm_vhost_port_link(struct se_portal_group *se_tpg, |
683bd967 | 1578 | struct se_lun *lun) |
057cbf49 | 1579 | { |
98718312 | 1580 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
057cbf49 NB |
1581 | struct tcm_vhost_tpg, se_tpg); |
1582 | ||
a6c9af87 AH |
1583 | mutex_lock(&tcm_vhost_mutex); |
1584 | ||
98718312 AH |
1585 | mutex_lock(&tpg->tv_tpg_mutex); |
1586 | tpg->tv_tpg_port_count++; | |
1587 | mutex_unlock(&tpg->tv_tpg_mutex); | |
057cbf49 | 1588 | |
98718312 | 1589 | tcm_vhost_hotplug(tpg, lun); |
a6c9af87 AH |
1590 | |
1591 | mutex_unlock(&tcm_vhost_mutex); | |
1592 | ||
057cbf49 NB |
1593 | return 0; |
1594 | } | |
1595 | ||
101998f6 | 1596 | static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg, |
683bd967 | 1597 | struct se_lun *lun) |
057cbf49 | 1598 | { |
98718312 | 1599 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
057cbf49 NB |
1600 | struct tcm_vhost_tpg, se_tpg); |
1601 | ||
a6c9af87 AH |
1602 | mutex_lock(&tcm_vhost_mutex); |
1603 | ||
98718312 AH |
1604 | mutex_lock(&tpg->tv_tpg_mutex); |
1605 | tpg->tv_tpg_port_count--; | |
1606 | mutex_unlock(&tpg->tv_tpg_mutex); | |
a6c9af87 | 1607 | |
98718312 | 1608 | tcm_vhost_hotunplug(tpg, lun); |
a6c9af87 AH |
1609 | |
1610 | mutex_unlock(&tcm_vhost_mutex); | |
057cbf49 NB |
1611 | } |
1612 | ||
683bd967 AH |
1613 | static struct se_node_acl * |
1614 | tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg, | |
1615 | struct config_group *group, | |
1616 | const char *name) | |
057cbf49 NB |
1617 | { |
1618 | struct se_node_acl *se_nacl, *se_nacl_new; | |
1619 | struct tcm_vhost_nacl *nacl; | |
1620 | u64 wwpn = 0; | |
1621 | u32 nexus_depth; | |
1622 | ||
1623 | /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0) | |
1624 | return ERR_PTR(-EINVAL); */ | |
1625 | se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg); | |
1626 | if (!se_nacl_new) | |
1627 | return ERR_PTR(-ENOMEM); | |
1628 | ||
1629 | nexus_depth = 1; | |
1630 | /* | |
1631 | * se_nacl_new may be released by core_tpg_add_initiator_node_acl() | |
1632 | * when converting a NodeACL from demo mode -> explict | |
1633 | */ | |
1634 | se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new, | |
1635 | name, nexus_depth); | |
1636 | if (IS_ERR(se_nacl)) { | |
1637 | tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new); | |
1638 | return se_nacl; | |
1639 | } | |
1640 | /* | |
1641 | * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN | |
1642 | */ | |
1643 | nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl); | |
1644 | nacl->iport_wwpn = wwpn; | |
1645 | ||
1646 | return se_nacl; | |
1647 | } | |
1648 | ||
1649 | static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl) | |
1650 | { | |
1651 | struct tcm_vhost_nacl *nacl = container_of(se_acl, | |
1652 | struct tcm_vhost_nacl, se_node_acl); | |
1653 | core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1); | |
1654 | kfree(nacl); | |
1655 | } | |
1656 | ||
98718312 | 1657 | static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg, |
683bd967 | 1658 | const char *name) |
057cbf49 NB |
1659 | { |
1660 | struct se_portal_group *se_tpg; | |
1661 | struct tcm_vhost_nexus *tv_nexus; | |
1662 | ||
98718312 AH |
1663 | mutex_lock(&tpg->tv_tpg_mutex); |
1664 | if (tpg->tpg_nexus) { | |
1665 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1666 | pr_debug("tpg->tpg_nexus already exists\n"); | |
057cbf49 NB |
1667 | return -EEXIST; |
1668 | } | |
98718312 | 1669 | se_tpg = &tpg->se_tpg; |
057cbf49 NB |
1670 | |
1671 | tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL); | |
1672 | if (!tv_nexus) { | |
98718312 | 1673 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 NB |
1674 | pr_err("Unable to allocate struct tcm_vhost_nexus\n"); |
1675 | return -ENOMEM; | |
1676 | } | |
1677 | /* | |
1678 | * Initialize the struct se_session pointer | |
1679 | */ | |
1680 | tv_nexus->tvn_se_sess = transport_init_session(); | |
1681 | if (IS_ERR(tv_nexus->tvn_se_sess)) { | |
98718312 | 1682 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 NB |
1683 | kfree(tv_nexus); |
1684 | return -ENOMEM; | |
1685 | } | |
1686 | /* | |
1687 | * Since we are running in 'demo mode' this call with generate a | |
1688 | * struct se_node_acl for the tcm_vhost struct se_portal_group with | |
1689 | * the SCSI Initiator port name of the passed configfs group 'name'. | |
1690 | */ | |
1691 | tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( | |
1692 | se_tpg, (unsigned char *)name); | |
1693 | if (!tv_nexus->tvn_se_sess->se_node_acl) { | |
98718312 | 1694 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 NB |
1695 | pr_debug("core_tpg_check_initiator_node_acl() failed" |
1696 | " for %s\n", name); | |
1697 | transport_free_session(tv_nexus->tvn_se_sess); | |
1698 | kfree(tv_nexus); | |
1699 | return -ENOMEM; | |
1700 | } | |
1701 | /* | |
101998f6 | 1702 | * Now register the TCM vhost virtual I_T Nexus as active with the |
057cbf49 NB |
1703 | * call to __transport_register_session() |
1704 | */ | |
1705 | __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, | |
1706 | tv_nexus->tvn_se_sess, tv_nexus); | |
98718312 | 1707 | tpg->tpg_nexus = tv_nexus; |
057cbf49 | 1708 | |
98718312 | 1709 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 NB |
1710 | return 0; |
1711 | } | |
1712 | ||
101998f6 | 1713 | static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg) |
057cbf49 NB |
1714 | { |
1715 | struct se_session *se_sess; | |
1716 | struct tcm_vhost_nexus *tv_nexus; | |
1717 | ||
1718 | mutex_lock(&tpg->tv_tpg_mutex); | |
1719 | tv_nexus = tpg->tpg_nexus; | |
1720 | if (!tv_nexus) { | |
1721 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1722 | return -ENODEV; | |
1723 | } | |
1724 | ||
1725 | se_sess = tv_nexus->tvn_se_sess; | |
1726 | if (!se_sess) { | |
1727 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1728 | return -ENODEV; | |
1729 | } | |
1730 | ||
101998f6 | 1731 | if (tpg->tv_tpg_port_count != 0) { |
057cbf49 | 1732 | mutex_unlock(&tpg->tv_tpg_mutex); |
101998f6 | 1733 | pr_err("Unable to remove TCM_vhost I_T Nexus with" |
057cbf49 | 1734 | " active TPG port count: %d\n", |
101998f6 NB |
1735 | tpg->tv_tpg_port_count); |
1736 | return -EBUSY; | |
057cbf49 NB |
1737 | } |
1738 | ||
101998f6 | 1739 | if (tpg->tv_tpg_vhost_count != 0) { |
057cbf49 | 1740 | mutex_unlock(&tpg->tv_tpg_mutex); |
101998f6 | 1741 | pr_err("Unable to remove TCM_vhost I_T Nexus with" |
057cbf49 | 1742 | " active TPG vhost count: %d\n", |
101998f6 NB |
1743 | tpg->tv_tpg_vhost_count); |
1744 | return -EBUSY; | |
057cbf49 NB |
1745 | } |
1746 | ||
101998f6 | 1747 | pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated" |
057cbf49 NB |
1748 | " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport), |
1749 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); | |
1750 | /* | |
101998f6 | 1751 | * Release the SCSI I_T Nexus to the emulated vhost Target Port |
057cbf49 NB |
1752 | */ |
1753 | transport_deregister_session(tv_nexus->tvn_se_sess); | |
1754 | tpg->tpg_nexus = NULL; | |
1755 | mutex_unlock(&tpg->tv_tpg_mutex); | |
1756 | ||
1757 | kfree(tv_nexus); | |
1758 | return 0; | |
1759 | } | |
1760 | ||
101998f6 | 1761 | static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg, |
683bd967 | 1762 | char *page) |
057cbf49 | 1763 | { |
98718312 | 1764 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
057cbf49 NB |
1765 | struct tcm_vhost_tpg, se_tpg); |
1766 | struct tcm_vhost_nexus *tv_nexus; | |
1767 | ssize_t ret; | |
1768 | ||
98718312 AH |
1769 | mutex_lock(&tpg->tv_tpg_mutex); |
1770 | tv_nexus = tpg->tpg_nexus; | |
057cbf49 | 1771 | if (!tv_nexus) { |
98718312 | 1772 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 NB |
1773 | return -ENODEV; |
1774 | } | |
1775 | ret = snprintf(page, PAGE_SIZE, "%s\n", | |
1776 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); | |
98718312 | 1777 | mutex_unlock(&tpg->tv_tpg_mutex); |
057cbf49 NB |
1778 | |
1779 | return ret; | |
1780 | } | |
1781 | ||
101998f6 | 1782 | static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg, |
683bd967 AH |
1783 | const char *page, |
1784 | size_t count) | |
057cbf49 | 1785 | { |
98718312 | 1786 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
057cbf49 | 1787 | struct tcm_vhost_tpg, se_tpg); |
98718312 | 1788 | struct tcm_vhost_tport *tport_wwn = tpg->tport; |
057cbf49 NB |
1789 | unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr; |
1790 | int ret; | |
1791 | /* | |
1792 | * Shutdown the active I_T nexus if 'NULL' is passed.. | |
1793 | */ | |
1794 | if (!strncmp(page, "NULL", 4)) { | |
98718312 | 1795 | ret = tcm_vhost_drop_nexus(tpg); |
057cbf49 NB |
1796 | return (!ret) ? count : ret; |
1797 | } | |
1798 | /* | |
1799 | * Otherwise make sure the passed virtual Initiator port WWN matches | |
1800 | * the fabric protocol_id set in tcm_vhost_make_tport(), and call | |
1801 | * tcm_vhost_make_nexus(). | |
1802 | */ | |
1803 | if (strlen(page) >= TCM_VHOST_NAMELEN) { | |
1804 | pr_err("Emulated NAA Sas Address: %s, exceeds" | |
1805 | " max: %d\n", page, TCM_VHOST_NAMELEN); | |
1806 | return -EINVAL; | |
1807 | } | |
1808 | snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page); | |
1809 | ||
1810 | ptr = strstr(i_port, "naa."); | |
1811 | if (ptr) { | |
1812 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { | |
1813 | pr_err("Passed SAS Initiator Port %s does not" | |
1814 | " match target port protoid: %s\n", i_port, | |
1815 | tcm_vhost_dump_proto_id(tport_wwn)); | |
1816 | return -EINVAL; | |
1817 | } | |
1818 | port_ptr = &i_port[0]; | |
1819 | goto check_newline; | |
1820 | } | |
1821 | ptr = strstr(i_port, "fc."); | |
1822 | if (ptr) { | |
1823 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { | |
1824 | pr_err("Passed FCP Initiator Port %s does not" | |
1825 | " match target port protoid: %s\n", i_port, | |
1826 | tcm_vhost_dump_proto_id(tport_wwn)); | |
1827 | return -EINVAL; | |
1828 | } | |
1829 | port_ptr = &i_port[3]; /* Skip over "fc." */ | |
1830 | goto check_newline; | |
1831 | } | |
1832 | ptr = strstr(i_port, "iqn."); | |
1833 | if (ptr) { | |
1834 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { | |
1835 | pr_err("Passed iSCSI Initiator Port %s does not" | |
1836 | " match target port protoid: %s\n", i_port, | |
1837 | tcm_vhost_dump_proto_id(tport_wwn)); | |
1838 | return -EINVAL; | |
1839 | } | |
1840 | port_ptr = &i_port[0]; | |
1841 | goto check_newline; | |
1842 | } | |
1843 | pr_err("Unable to locate prefix for emulated Initiator Port:" | |
1844 | " %s\n", i_port); | |
1845 | return -EINVAL; | |
1846 | /* | |
1847 | * Clear any trailing newline for the NAA WWN | |
1848 | */ | |
1849 | check_newline: | |
1850 | if (i_port[strlen(i_port)-1] == '\n') | |
1851 | i_port[strlen(i_port)-1] = '\0'; | |
1852 | ||
98718312 | 1853 | ret = tcm_vhost_make_nexus(tpg, port_ptr); |
057cbf49 NB |
1854 | if (ret < 0) |
1855 | return ret; | |
1856 | ||
1857 | return count; | |
1858 | } | |
1859 | ||
1860 | TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR); | |
1861 | ||
1862 | static struct configfs_attribute *tcm_vhost_tpg_attrs[] = { | |
1863 | &tcm_vhost_tpg_nexus.attr, | |
1864 | NULL, | |
1865 | }; | |
1866 | ||
683bd967 AH |
1867 | static struct se_portal_group * |
1868 | tcm_vhost_make_tpg(struct se_wwn *wwn, | |
1869 | struct config_group *group, | |
1870 | const char *name) | |
057cbf49 NB |
1871 | { |
1872 | struct tcm_vhost_tport *tport = container_of(wwn, | |
1873 | struct tcm_vhost_tport, tport_wwn); | |
1874 | ||
1875 | struct tcm_vhost_tpg *tpg; | |
1876 | unsigned long tpgt; | |
1877 | int ret; | |
1878 | ||
1879 | if (strstr(name, "tpgt_") != name) | |
1880 | return ERR_PTR(-EINVAL); | |
1881 | if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX) | |
1882 | return ERR_PTR(-EINVAL); | |
1883 | ||
1884 | tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL); | |
1885 | if (!tpg) { | |
1886 | pr_err("Unable to allocate struct tcm_vhost_tpg"); | |
1887 | return ERR_PTR(-ENOMEM); | |
1888 | } | |
1889 | mutex_init(&tpg->tv_tpg_mutex); | |
1890 | INIT_LIST_HEAD(&tpg->tv_tpg_list); | |
1891 | tpg->tport = tport; | |
1892 | tpg->tport_tpgt = tpgt; | |
1893 | ||
1894 | ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn, | |
1895 | &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL); | |
1896 | if (ret < 0) { | |
1897 | kfree(tpg); | |
1898 | return NULL; | |
1899 | } | |
1900 | mutex_lock(&tcm_vhost_mutex); | |
1901 | list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list); | |
1902 | mutex_unlock(&tcm_vhost_mutex); | |
1903 | ||
1904 | return &tpg->se_tpg; | |
1905 | } | |
1906 | ||
1907 | static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg) | |
1908 | { | |
1909 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, | |
1910 | struct tcm_vhost_tpg, se_tpg); | |
1911 | ||
1912 | mutex_lock(&tcm_vhost_mutex); | |
1913 | list_del(&tpg->tv_tpg_list); | |
1914 | mutex_unlock(&tcm_vhost_mutex); | |
1915 | /* | |
101998f6 | 1916 | * Release the virtual I_T Nexus for this vhost TPG |
057cbf49 NB |
1917 | */ |
1918 | tcm_vhost_drop_nexus(tpg); | |
1919 | /* | |
1920 | * Deregister the se_tpg from TCM.. | |
1921 | */ | |
1922 | core_tpg_deregister(se_tpg); | |
1923 | kfree(tpg); | |
1924 | } | |
1925 | ||
683bd967 AH |
1926 | static struct se_wwn * |
1927 | tcm_vhost_make_tport(struct target_fabric_configfs *tf, | |
1928 | struct config_group *group, | |
1929 | const char *name) | |
057cbf49 NB |
1930 | { |
1931 | struct tcm_vhost_tport *tport; | |
1932 | char *ptr; | |
1933 | u64 wwpn = 0; | |
1934 | int off = 0; | |
1935 | ||
1936 | /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0) | |
1937 | return ERR_PTR(-EINVAL); */ | |
1938 | ||
1939 | tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL); | |
1940 | if (!tport) { | |
1941 | pr_err("Unable to allocate struct tcm_vhost_tport"); | |
1942 | return ERR_PTR(-ENOMEM); | |
1943 | } | |
1944 | tport->tport_wwpn = wwpn; | |
1945 | /* | |
1946 | * Determine the emulated Protocol Identifier and Target Port Name | |
1947 | * based on the incoming configfs directory name. | |
1948 | */ | |
1949 | ptr = strstr(name, "naa."); | |
1950 | if (ptr) { | |
1951 | tport->tport_proto_id = SCSI_PROTOCOL_SAS; | |
1952 | goto check_len; | |
1953 | } | |
1954 | ptr = strstr(name, "fc."); | |
1955 | if (ptr) { | |
1956 | tport->tport_proto_id = SCSI_PROTOCOL_FCP; | |
1957 | off = 3; /* Skip over "fc." */ | |
1958 | goto check_len; | |
1959 | } | |
1960 | ptr = strstr(name, "iqn."); | |
1961 | if (ptr) { | |
1962 | tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; | |
1963 | goto check_len; | |
1964 | } | |
1965 | ||
1966 | pr_err("Unable to locate prefix for emulated Target Port:" | |
1967 | " %s\n", name); | |
1968 | kfree(tport); | |
1969 | return ERR_PTR(-EINVAL); | |
1970 | ||
1971 | check_len: | |
1972 | if (strlen(name) >= TCM_VHOST_NAMELEN) { | |
1973 | pr_err("Emulated %s Address: %s, exceeds" | |
1974 | " max: %d\n", name, tcm_vhost_dump_proto_id(tport), | |
1975 | TCM_VHOST_NAMELEN); | |
1976 | kfree(tport); | |
1977 | return ERR_PTR(-EINVAL); | |
1978 | } | |
1979 | snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]); | |
1980 | ||
1981 | pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target" | |
1982 | " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name); | |
1983 | ||
1984 | return &tport->tport_wwn; | |
1985 | } | |
1986 | ||
1987 | static void tcm_vhost_drop_tport(struct se_wwn *wwn) | |
1988 | { | |
1989 | struct tcm_vhost_tport *tport = container_of(wwn, | |
1990 | struct tcm_vhost_tport, tport_wwn); | |
1991 | ||
1992 | pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target" | |
1993 | " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), | |
1994 | tport->tport_name); | |
1995 | ||
1996 | kfree(tport); | |
1997 | } | |
1998 | ||
683bd967 AH |
1999 | static ssize_t |
2000 | tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf, | |
2001 | char *page) | |
057cbf49 NB |
2002 | { |
2003 | return sprintf(page, "TCM_VHOST fabric module %s on %s/%s" | |
2004 | "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, | |
2005 | utsname()->machine); | |
2006 | } | |
2007 | ||
2008 | TF_WWN_ATTR_RO(tcm_vhost, version); | |
2009 | ||
2010 | static struct configfs_attribute *tcm_vhost_wwn_attrs[] = { | |
2011 | &tcm_vhost_wwn_version.attr, | |
2012 | NULL, | |
2013 | }; | |
2014 | ||
2015 | static struct target_core_fabric_ops tcm_vhost_ops = { | |
2016 | .get_fabric_name = tcm_vhost_get_fabric_name, | |
2017 | .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident, | |
2018 | .tpg_get_wwn = tcm_vhost_get_fabric_wwn, | |
2019 | .tpg_get_tag = tcm_vhost_get_tag, | |
2020 | .tpg_get_default_depth = tcm_vhost_get_default_depth, | |
2021 | .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id, | |
2022 | .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len, | |
2023 | .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id, | |
2024 | .tpg_check_demo_mode = tcm_vhost_check_true, | |
2025 | .tpg_check_demo_mode_cache = tcm_vhost_check_true, | |
2026 | .tpg_check_demo_mode_write_protect = tcm_vhost_check_false, | |
2027 | .tpg_check_prod_mode_write_protect = tcm_vhost_check_false, | |
2028 | .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl, | |
2029 | .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl, | |
2030 | .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index, | |
2031 | .release_cmd = tcm_vhost_release_cmd, | |
2032 | .shutdown_session = tcm_vhost_shutdown_session, | |
2033 | .close_session = tcm_vhost_close_session, | |
2034 | .sess_get_index = tcm_vhost_sess_get_index, | |
2035 | .sess_get_initiator_sid = NULL, | |
2036 | .write_pending = tcm_vhost_write_pending, | |
2037 | .write_pending_status = tcm_vhost_write_pending_status, | |
2038 | .set_default_node_attributes = tcm_vhost_set_default_node_attrs, | |
2039 | .get_task_tag = tcm_vhost_get_task_tag, | |
2040 | .get_cmd_state = tcm_vhost_get_cmd_state, | |
2041 | .queue_data_in = tcm_vhost_queue_data_in, | |
2042 | .queue_status = tcm_vhost_queue_status, | |
2043 | .queue_tm_rsp = tcm_vhost_queue_tm_rsp, | |
057cbf49 NB |
2044 | /* |
2045 | * Setup callers for generic logic in target_core_fabric_configfs.c | |
2046 | */ | |
2047 | .fabric_make_wwn = tcm_vhost_make_tport, | |
2048 | .fabric_drop_wwn = tcm_vhost_drop_tport, | |
2049 | .fabric_make_tpg = tcm_vhost_make_tpg, | |
2050 | .fabric_drop_tpg = tcm_vhost_drop_tpg, | |
2051 | .fabric_post_link = tcm_vhost_port_link, | |
2052 | .fabric_pre_unlink = tcm_vhost_port_unlink, | |
2053 | .fabric_make_np = NULL, | |
2054 | .fabric_drop_np = NULL, | |
2055 | .fabric_make_nodeacl = tcm_vhost_make_nodeacl, | |
2056 | .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl, | |
2057 | }; | |
2058 | ||
2059 | static int tcm_vhost_register_configfs(void) | |
2060 | { | |
2061 | struct target_fabric_configfs *fabric; | |
2062 | int ret; | |
2063 | ||
2064 | pr_debug("TCM_VHOST fabric module %s on %s/%s" | |
2065 | " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, | |
2066 | utsname()->machine); | |
2067 | /* | |
2068 | * Register the top level struct config_item_type with TCM core | |
2069 | */ | |
2070 | fabric = target_fabric_configfs_init(THIS_MODULE, "vhost"); | |
2071 | if (IS_ERR(fabric)) { | |
2072 | pr_err("target_fabric_configfs_init() failed\n"); | |
2073 | return PTR_ERR(fabric); | |
2074 | } | |
2075 | /* | |
2076 | * Setup fabric->tf_ops from our local tcm_vhost_ops | |
2077 | */ | |
2078 | fabric->tf_ops = tcm_vhost_ops; | |
2079 | /* | |
2080 | * Setup default attribute lists for various fabric->tf_cit_tmpl | |
2081 | */ | |
2082 | TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs; | |
2083 | TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs; | |
2084 | TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL; | |
2085 | TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL; | |
2086 | TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL; | |
2087 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL; | |
2088 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; | |
2089 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL; | |
2090 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL; | |
2091 | /* | |
2092 | * Register the fabric for use within TCM | |
2093 | */ | |
2094 | ret = target_fabric_configfs_register(fabric); | |
2095 | if (ret < 0) { | |
2096 | pr_err("target_fabric_configfs_register() failed" | |
2097 | " for TCM_VHOST\n"); | |
2098 | return ret; | |
2099 | } | |
2100 | /* | |
2101 | * Setup our local pointer to *fabric | |
2102 | */ | |
2103 | tcm_vhost_fabric_configfs = fabric; | |
2104 | pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n"); | |
2105 | return 0; | |
2106 | }; | |
2107 | ||
2108 | static void tcm_vhost_deregister_configfs(void) | |
2109 | { | |
2110 | if (!tcm_vhost_fabric_configfs) | |
2111 | return; | |
2112 | ||
2113 | target_fabric_configfs_deregister(tcm_vhost_fabric_configfs); | |
2114 | tcm_vhost_fabric_configfs = NULL; | |
2115 | pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n"); | |
2116 | }; | |
2117 | ||
2118 | static int __init tcm_vhost_init(void) | |
2119 | { | |
2120 | int ret = -ENOMEM; | |
101998f6 NB |
2121 | /* |
2122 | * Use our own dedicated workqueue for submitting I/O into | |
2123 | * target core to avoid contention within system_wq. | |
2124 | */ | |
057cbf49 NB |
2125 | tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0); |
2126 | if (!tcm_vhost_workqueue) | |
2127 | goto out; | |
2128 | ||
2129 | ret = vhost_scsi_register(); | |
2130 | if (ret < 0) | |
2131 | goto out_destroy_workqueue; | |
2132 | ||
2133 | ret = tcm_vhost_register_configfs(); | |
2134 | if (ret < 0) | |
2135 | goto out_vhost_scsi_deregister; | |
2136 | ||
2137 | return 0; | |
2138 | ||
2139 | out_vhost_scsi_deregister: | |
2140 | vhost_scsi_deregister(); | |
2141 | out_destroy_workqueue: | |
2142 | destroy_workqueue(tcm_vhost_workqueue); | |
2143 | out: | |
2144 | return ret; | |
2145 | }; | |
2146 | ||
2147 | static void tcm_vhost_exit(void) | |
2148 | { | |
2149 | tcm_vhost_deregister_configfs(); | |
2150 | vhost_scsi_deregister(); | |
2151 | destroy_workqueue(tcm_vhost_workqueue); | |
2152 | }; | |
2153 | ||
181c04a3 MT |
2154 | MODULE_DESCRIPTION("VHOST_SCSI series fabric driver"); |
2155 | MODULE_ALIAS("tcm_vhost"); | |
057cbf49 NB |
2156 | MODULE_LICENSE("GPL"); |
2157 | module_init(tcm_vhost_init); | |
2158 | module_exit(tcm_vhost_exit); |