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