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