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