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