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