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