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