Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Xen SCSI backend driver |
| 3 | * |
| 4 | * Copyright (c) 2008, FUJITSU Limited |
| 5 | * |
| 6 | * Based on the blkback driver code. |
| 7 | * Adaption to kernel taget core infrastructure taken from vhost/scsi.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License version 2 |
| 11 | * as published by the Free Software Foundation; or, when distributed |
| 12 | * separately from the Linux kernel or incorporated into other |
| 13 | * software packages, subject to the following license: |
| 14 | * |
| 15 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | * of this source file (the "Software"), to deal in the Software without |
| 17 | * restriction, including without limitation the rights to use, copy, modify, |
| 18 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 19 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 20 | * the following conditions: |
| 21 | * |
| 22 | * The above copyright notice and this permission notice shall be included in |
| 23 | * all copies or substantial portions of the Software. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 30 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 31 | * IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 34 | #define pr_fmt(fmt) "xen-pvscsi: " fmt |
| 35 | |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 36 | #include <stdarg.h> |
| 37 | |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/utsname.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/wait.h> |
| 43 | #include <linux/sched.h> |
| 44 | #include <linux/list.h> |
| 45 | #include <linux/gfp.h> |
| 46 | #include <linux/delay.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/configfs.h> |
| 49 | |
| 50 | #include <generated/utsrelease.h> |
| 51 | |
Hannes Reinecke | 2dd951e | 2015-01-08 07:43:48 +0100 | [diff] [blame] | 52 | #include <scsi/scsi.h> |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 53 | #include <scsi/scsi_dbg.h> |
| 54 | #include <scsi/scsi_eh.h> |
| 55 | #include <scsi/scsi_tcq.h> |
| 56 | |
| 57 | #include <target/target_core_base.h> |
| 58 | #include <target/target_core_fabric.h> |
| 59 | #include <target/target_core_configfs.h> |
| 60 | #include <target/target_core_fabric_configfs.h> |
| 61 | |
| 62 | #include <asm/hypervisor.h> |
| 63 | |
| 64 | #include <xen/xen.h> |
| 65 | #include <xen/balloon.h> |
| 66 | #include <xen/events.h> |
| 67 | #include <xen/xenbus.h> |
| 68 | #include <xen/grant_table.h> |
| 69 | #include <xen/page.h> |
| 70 | |
| 71 | #include <xen/interface/grant_table.h> |
| 72 | #include <xen/interface/io/vscsiif.h> |
| 73 | |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 74 | #define VSCSI_VERSION "v0.1" |
| 75 | #define VSCSI_NAMELEN 32 |
| 76 | |
| 77 | struct ids_tuple { |
| 78 | unsigned int hst; /* host */ |
| 79 | unsigned int chn; /* channel */ |
| 80 | unsigned int tgt; /* target */ |
| 81 | unsigned int lun; /* LUN */ |
| 82 | }; |
| 83 | |
| 84 | struct v2p_entry { |
| 85 | struct ids_tuple v; /* translate from */ |
| 86 | struct scsiback_tpg *tpg; /* translate to */ |
| 87 | unsigned int lun; |
| 88 | struct kref kref; |
| 89 | struct list_head l; |
| 90 | }; |
| 91 | |
| 92 | struct vscsibk_info { |
| 93 | struct xenbus_device *dev; |
| 94 | |
| 95 | domid_t domid; |
| 96 | unsigned int irq; |
| 97 | |
| 98 | struct vscsiif_back_ring ring; |
| 99 | int ring_error; |
| 100 | |
| 101 | spinlock_t ring_lock; |
| 102 | atomic_t nr_unreplied_reqs; |
| 103 | |
| 104 | spinlock_t v2p_lock; |
| 105 | struct list_head v2p_entry_lists; |
| 106 | |
| 107 | wait_queue_head_t waiting_to_free; |
| 108 | }; |
| 109 | |
| 110 | /* theoretical maximum of grants for one request */ |
| 111 | #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE) |
| 112 | |
| 113 | /* |
| 114 | * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one |
| 115 | * call to map/unmap grants. Don't choose it too large, as there are arrays |
| 116 | * with VSCSI_GRANT_BATCH elements allocated on the stack. |
| 117 | */ |
| 118 | #define VSCSI_GRANT_BATCH 16 |
| 119 | |
| 120 | struct vscsibk_pend { |
| 121 | uint16_t rqid; |
| 122 | |
| 123 | uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE]; |
| 124 | uint8_t cmd_len; |
| 125 | |
| 126 | uint8_t sc_data_direction; |
| 127 | uint16_t n_sg; /* real length of SG list */ |
| 128 | uint16_t n_grants; /* SG pages and potentially SG list */ |
| 129 | uint32_t data_len; |
| 130 | uint32_t result; |
| 131 | |
| 132 | struct vscsibk_info *info; |
| 133 | struct v2p_entry *v2p; |
| 134 | struct scatterlist *sgl; |
| 135 | |
| 136 | uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE]; |
| 137 | |
| 138 | grant_handle_t grant_handles[VSCSI_MAX_GRANTS]; |
| 139 | struct page *pages[VSCSI_MAX_GRANTS]; |
| 140 | |
| 141 | struct se_cmd se_cmd; |
| 142 | }; |
| 143 | |
| 144 | struct scsiback_tmr { |
| 145 | atomic_t tmr_complete; |
| 146 | wait_queue_head_t tmr_wait; |
| 147 | }; |
| 148 | |
| 149 | struct scsiback_nexus { |
| 150 | /* Pointer to TCM session for I_T Nexus */ |
| 151 | struct se_session *tvn_se_sess; |
| 152 | }; |
| 153 | |
| 154 | struct scsiback_tport { |
| 155 | /* SCSI protocol the tport is providing */ |
| 156 | u8 tport_proto_id; |
| 157 | /* Binary World Wide unique Port Name for pvscsi Target port */ |
| 158 | u64 tport_wwpn; |
| 159 | /* ASCII formatted WWPN for pvscsi Target port */ |
| 160 | char tport_name[VSCSI_NAMELEN]; |
| 161 | /* Returned by scsiback_make_tport() */ |
| 162 | struct se_wwn tport_wwn; |
| 163 | }; |
| 164 | |
| 165 | struct scsiback_tpg { |
| 166 | /* scsiback port target portal group tag for TCM */ |
| 167 | u16 tport_tpgt; |
| 168 | /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */ |
| 169 | int tv_tpg_port_count; |
| 170 | /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */ |
| 171 | int tv_tpg_fe_count; |
| 172 | /* list for scsiback_list */ |
| 173 | struct list_head tv_tpg_list; |
| 174 | /* Used to protect access for tpg_nexus */ |
| 175 | struct mutex tv_tpg_mutex; |
| 176 | /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */ |
| 177 | struct scsiback_nexus *tpg_nexus; |
| 178 | /* Pointer back to scsiback_tport */ |
| 179 | struct scsiback_tport *tport; |
| 180 | /* Returned by scsiback_make_tpg() */ |
| 181 | struct se_portal_group se_tpg; |
| 182 | /* alias used in xenstore */ |
| 183 | char param_alias[VSCSI_NAMELEN]; |
| 184 | /* list of info structures related to this target portal group */ |
| 185 | struct list_head info_list; |
| 186 | }; |
| 187 | |
| 188 | #define SCSIBACK_INVALID_HANDLE (~0) |
| 189 | |
| 190 | static bool log_print_stat; |
| 191 | module_param(log_print_stat, bool, 0644); |
| 192 | |
| 193 | static int scsiback_max_buffer_pages = 1024; |
| 194 | module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644); |
| 195 | MODULE_PARM_DESC(max_buffer_pages, |
| 196 | "Maximum number of free pages to keep in backend buffer"); |
| 197 | |
| 198 | static struct kmem_cache *scsiback_cachep; |
| 199 | static DEFINE_SPINLOCK(free_pages_lock); |
| 200 | static int free_pages_num; |
| 201 | static LIST_HEAD(scsiback_free_pages); |
| 202 | |
| 203 | /* Global spinlock to protect scsiback TPG list */ |
| 204 | static DEFINE_MUTEX(scsiback_mutex); |
| 205 | static LIST_HEAD(scsiback_list); |
| 206 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 207 | static const struct target_core_fabric_ops scsiback_ops; |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 208 | |
| 209 | static void scsiback_get(struct vscsibk_info *info) |
| 210 | { |
| 211 | atomic_inc(&info->nr_unreplied_reqs); |
| 212 | } |
| 213 | |
| 214 | static void scsiback_put(struct vscsibk_info *info) |
| 215 | { |
| 216 | if (atomic_dec_and_test(&info->nr_unreplied_reqs)) |
| 217 | wake_up(&info->waiting_to_free); |
| 218 | } |
| 219 | |
| 220 | static void put_free_pages(struct page **page, int num) |
| 221 | { |
| 222 | unsigned long flags; |
| 223 | int i = free_pages_num + num, n = num; |
| 224 | |
| 225 | if (num == 0) |
| 226 | return; |
| 227 | if (i > scsiback_max_buffer_pages) { |
| 228 | n = min(num, i - scsiback_max_buffer_pages); |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 229 | gnttab_free_pages(n, page + num - n); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 230 | n = num - n; |
| 231 | } |
| 232 | spin_lock_irqsave(&free_pages_lock, flags); |
| 233 | for (i = 0; i < n; i++) |
| 234 | list_add(&page[i]->lru, &scsiback_free_pages); |
| 235 | free_pages_num += n; |
| 236 | spin_unlock_irqrestore(&free_pages_lock, flags); |
| 237 | } |
| 238 | |
| 239 | static int get_free_page(struct page **page) |
| 240 | { |
| 241 | unsigned long flags; |
| 242 | |
| 243 | spin_lock_irqsave(&free_pages_lock, flags); |
| 244 | if (list_empty(&scsiback_free_pages)) { |
| 245 | spin_unlock_irqrestore(&free_pages_lock, flags); |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 246 | return gnttab_alloc_pages(1, page); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 247 | } |
| 248 | page[0] = list_first_entry(&scsiback_free_pages, struct page, lru); |
| 249 | list_del(&page[0]->lru); |
| 250 | free_pages_num--; |
| 251 | spin_unlock_irqrestore(&free_pages_lock, flags); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static unsigned long vaddr_page(struct page *page) |
| 256 | { |
| 257 | unsigned long pfn = page_to_pfn(page); |
| 258 | |
| 259 | return (unsigned long)pfn_to_kaddr(pfn); |
| 260 | } |
| 261 | |
| 262 | static unsigned long vaddr(struct vscsibk_pend *req, int seg) |
| 263 | { |
| 264 | return vaddr_page(req->pages[seg]); |
| 265 | } |
| 266 | |
| 267 | static void scsiback_print_status(char *sense_buffer, int errors, |
| 268 | struct vscsibk_pend *pending_req) |
| 269 | { |
| 270 | struct scsiback_tpg *tpg = pending_req->v2p->tpg; |
| 271 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 272 | pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 273 | tpg->tport->tport_name, pending_req->v2p->lun, |
| 274 | pending_req->cmnd[0], status_byte(errors), msg_byte(errors), |
| 275 | host_byte(errors), driver_byte(errors)); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static void scsiback_fast_flush_area(struct vscsibk_pend *req) |
| 279 | { |
| 280 | struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH]; |
| 281 | struct page *pages[VSCSI_GRANT_BATCH]; |
| 282 | unsigned int i, invcount = 0; |
| 283 | grant_handle_t handle; |
| 284 | int err; |
| 285 | |
| 286 | kfree(req->sgl); |
| 287 | req->sgl = NULL; |
| 288 | req->n_sg = 0; |
| 289 | |
| 290 | if (!req->n_grants) |
| 291 | return; |
| 292 | |
| 293 | for (i = 0; i < req->n_grants; i++) { |
| 294 | handle = req->grant_handles[i]; |
| 295 | if (handle == SCSIBACK_INVALID_HANDLE) |
| 296 | continue; |
| 297 | gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i), |
| 298 | GNTMAP_host_map, handle); |
| 299 | req->grant_handles[i] = SCSIBACK_INVALID_HANDLE; |
| 300 | pages[invcount] = req->pages[i]; |
| 301 | put_page(pages[invcount]); |
| 302 | invcount++; |
| 303 | if (invcount < VSCSI_GRANT_BATCH) |
| 304 | continue; |
| 305 | err = gnttab_unmap_refs(unmap, NULL, pages, invcount); |
| 306 | BUG_ON(err); |
| 307 | invcount = 0; |
| 308 | } |
| 309 | |
| 310 | if (invcount) { |
| 311 | err = gnttab_unmap_refs(unmap, NULL, pages, invcount); |
| 312 | BUG_ON(err); |
| 313 | } |
| 314 | |
| 315 | put_free_pages(req->pages, req->n_grants); |
| 316 | req->n_grants = 0; |
| 317 | } |
| 318 | |
| 319 | static void scsiback_free_translation_entry(struct kref *kref) |
| 320 | { |
| 321 | struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref); |
| 322 | struct scsiback_tpg *tpg = entry->tpg; |
| 323 | |
| 324 | mutex_lock(&tpg->tv_tpg_mutex); |
| 325 | tpg->tv_tpg_fe_count--; |
| 326 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 327 | |
| 328 | kfree(entry); |
| 329 | } |
| 330 | |
| 331 | static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result, |
| 332 | uint32_t resid, struct vscsibk_pend *pending_req) |
| 333 | { |
| 334 | struct vscsiif_response *ring_res; |
| 335 | struct vscsibk_info *info = pending_req->info; |
| 336 | int notify; |
| 337 | struct scsi_sense_hdr sshdr; |
| 338 | unsigned long flags; |
| 339 | unsigned len; |
| 340 | |
| 341 | spin_lock_irqsave(&info->ring_lock, flags); |
| 342 | |
| 343 | ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt); |
| 344 | info->ring.rsp_prod_pvt++; |
| 345 | |
| 346 | ring_res->rslt = result; |
| 347 | ring_res->rqid = pending_req->rqid; |
| 348 | |
| 349 | if (sense_buffer != NULL && |
| 350 | scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE, |
| 351 | &sshdr)) { |
| 352 | len = min_t(unsigned, 8 + sense_buffer[7], |
| 353 | VSCSIIF_SENSE_BUFFERSIZE); |
| 354 | memcpy(ring_res->sense_buffer, sense_buffer, len); |
| 355 | ring_res->sense_len = len; |
| 356 | } else { |
| 357 | ring_res->sense_len = 0; |
| 358 | } |
| 359 | |
| 360 | ring_res->residual_len = resid; |
| 361 | |
| 362 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify); |
| 363 | spin_unlock_irqrestore(&info->ring_lock, flags); |
| 364 | |
| 365 | if (notify) |
| 366 | notify_remote_via_irq(info->irq); |
| 367 | |
| 368 | if (pending_req->v2p) |
| 369 | kref_put(&pending_req->v2p->kref, |
| 370 | scsiback_free_translation_entry); |
| 371 | } |
| 372 | |
| 373 | static void scsiback_cmd_done(struct vscsibk_pend *pending_req) |
| 374 | { |
| 375 | struct vscsibk_info *info = pending_req->info; |
| 376 | unsigned char *sense_buffer; |
| 377 | unsigned int resid; |
| 378 | int errors; |
| 379 | |
| 380 | sense_buffer = pending_req->sense_buffer; |
| 381 | resid = pending_req->se_cmd.residual_count; |
| 382 | errors = pending_req->result; |
| 383 | |
| 384 | if (errors && log_print_stat) |
| 385 | scsiback_print_status(sense_buffer, errors, pending_req); |
| 386 | |
| 387 | scsiback_fast_flush_area(pending_req); |
| 388 | scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req); |
| 389 | scsiback_put(info); |
| 390 | } |
| 391 | |
| 392 | static void scsiback_cmd_exec(struct vscsibk_pend *pending_req) |
| 393 | { |
| 394 | struct se_cmd *se_cmd = &pending_req->se_cmd; |
| 395 | struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess; |
| 396 | int rc; |
| 397 | |
| 398 | memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE); |
| 399 | |
| 400 | memset(se_cmd, 0, sizeof(*se_cmd)); |
| 401 | |
| 402 | scsiback_get(pending_req->info); |
| 403 | rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd, |
| 404 | pending_req->sense_buffer, pending_req->v2p->lun, |
| 405 | pending_req->data_len, 0, |
| 406 | pending_req->sc_data_direction, 0, |
| 407 | pending_req->sgl, pending_req->n_sg, |
| 408 | NULL, 0, NULL, 0); |
| 409 | if (rc < 0) { |
| 410 | transport_send_check_condition_and_sense(se_cmd, |
| 411 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); |
| 412 | transport_generic_free_cmd(se_cmd, 0); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map, |
| 417 | struct page **pg, grant_handle_t *grant, int cnt) |
| 418 | { |
| 419 | int err, i; |
| 420 | |
| 421 | if (!cnt) |
| 422 | return 0; |
| 423 | |
| 424 | err = gnttab_map_refs(map, NULL, pg, cnt); |
| 425 | BUG_ON(err); |
| 426 | for (i = 0; i < cnt; i++) { |
| 427 | if (unlikely(map[i].status != GNTST_okay)) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 428 | pr_err("invalid buffer -- could not remap it\n"); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 429 | map[i].handle = SCSIBACK_INVALID_HANDLE; |
| 430 | err = -ENOMEM; |
| 431 | } else { |
| 432 | get_page(pg[i]); |
| 433 | } |
| 434 | grant[i] = map[i].handle; |
| 435 | } |
| 436 | return err; |
| 437 | } |
| 438 | |
| 439 | static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req, |
| 440 | struct scsiif_request_segment *seg, struct page **pg, |
| 441 | grant_handle_t *grant, int cnt, u32 flags) |
| 442 | { |
| 443 | int mapcount = 0, i, err = 0; |
| 444 | struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH]; |
| 445 | struct vscsibk_info *info = pending_req->info; |
| 446 | |
| 447 | for (i = 0; i < cnt; i++) { |
| 448 | if (get_free_page(pg + mapcount)) { |
| 449 | put_free_pages(pg, mapcount); |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 450 | pr_err("no grant page\n"); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 451 | return -ENOMEM; |
| 452 | } |
| 453 | gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]), |
| 454 | flags, seg[i].gref, info->domid); |
| 455 | mapcount++; |
| 456 | if (mapcount < VSCSI_GRANT_BATCH) |
| 457 | continue; |
| 458 | err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount); |
| 459 | pg += mapcount; |
| 460 | grant += mapcount; |
| 461 | pending_req->n_grants += mapcount; |
| 462 | if (err) |
| 463 | return err; |
| 464 | mapcount = 0; |
| 465 | } |
| 466 | err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount); |
| 467 | pending_req->n_grants += mapcount; |
| 468 | return err; |
| 469 | } |
| 470 | |
| 471 | static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req, |
| 472 | struct vscsibk_pend *pending_req) |
| 473 | { |
| 474 | u32 flags; |
| 475 | int i, err, n_segs, i_seg = 0; |
| 476 | struct page **pg; |
| 477 | struct scsiif_request_segment *seg; |
| 478 | unsigned long end_seg = 0; |
| 479 | unsigned int nr_segments = (unsigned int)ring_req->nr_segments; |
| 480 | unsigned int nr_sgl = 0; |
| 481 | struct scatterlist *sg; |
| 482 | grant_handle_t *grant; |
| 483 | |
| 484 | pending_req->n_sg = 0; |
| 485 | pending_req->n_grants = 0; |
| 486 | pending_req->data_len = 0; |
| 487 | |
| 488 | nr_segments &= ~VSCSIIF_SG_GRANT; |
| 489 | if (!nr_segments) |
| 490 | return 0; |
| 491 | |
| 492 | if (nr_segments > VSCSIIF_SG_TABLESIZE) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 493 | pr_debug("invalid parameter nr_seg = %d\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 494 | ring_req->nr_segments); |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | if (ring_req->nr_segments & VSCSIIF_SG_GRANT) { |
| 499 | err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg, |
| 500 | pending_req->pages, pending_req->grant_handles, |
| 501 | nr_segments, GNTMAP_host_map | GNTMAP_readonly); |
| 502 | if (err) |
| 503 | return err; |
| 504 | nr_sgl = nr_segments; |
| 505 | nr_segments = 0; |
| 506 | for (i = 0; i < nr_sgl; i++) { |
| 507 | n_segs = ring_req->seg[i].length / |
| 508 | sizeof(struct scsiif_request_segment); |
| 509 | if ((unsigned)ring_req->seg[i].offset + |
| 510 | (unsigned)ring_req->seg[i].length > PAGE_SIZE || |
| 511 | n_segs * sizeof(struct scsiif_request_segment) != |
| 512 | ring_req->seg[i].length) |
| 513 | return -EINVAL; |
| 514 | nr_segments += n_segs; |
| 515 | } |
| 516 | if (nr_segments > SG_ALL) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 517 | pr_debug("invalid nr_seg = %d\n", nr_segments); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 518 | return -EINVAL; |
| 519 | } |
| 520 | } |
| 521 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 522 | /* free of (sgl) in fast_flush_area() */ |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 523 | pending_req->sgl = kmalloc_array(nr_segments, |
| 524 | sizeof(struct scatterlist), GFP_KERNEL); |
| 525 | if (!pending_req->sgl) |
| 526 | return -ENOMEM; |
| 527 | |
| 528 | sg_init_table(pending_req->sgl, nr_segments); |
| 529 | pending_req->n_sg = nr_segments; |
| 530 | |
| 531 | flags = GNTMAP_host_map; |
| 532 | if (pending_req->sc_data_direction == DMA_TO_DEVICE) |
| 533 | flags |= GNTMAP_readonly; |
| 534 | |
| 535 | pg = pending_req->pages + nr_sgl; |
| 536 | grant = pending_req->grant_handles + nr_sgl; |
| 537 | if (!nr_sgl) { |
| 538 | seg = ring_req->seg; |
| 539 | err = scsiback_gnttab_data_map_list(pending_req, seg, |
| 540 | pg, grant, nr_segments, flags); |
| 541 | if (err) |
| 542 | return err; |
| 543 | } else { |
| 544 | for (i = 0; i < nr_sgl; i++) { |
| 545 | seg = (struct scsiif_request_segment *)( |
| 546 | vaddr(pending_req, i) + ring_req->seg[i].offset); |
| 547 | n_segs = ring_req->seg[i].length / |
| 548 | sizeof(struct scsiif_request_segment); |
| 549 | err = scsiback_gnttab_data_map_list(pending_req, seg, |
| 550 | pg, grant, n_segs, flags); |
| 551 | if (err) |
| 552 | return err; |
| 553 | pg += n_segs; |
| 554 | grant += n_segs; |
| 555 | } |
| 556 | end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset; |
| 557 | seg = (struct scsiif_request_segment *)end_seg; |
| 558 | end_seg += ring_req->seg[0].length; |
| 559 | pg = pending_req->pages + nr_sgl; |
| 560 | } |
| 561 | |
| 562 | for_each_sg(pending_req->sgl, sg, nr_segments, i) { |
| 563 | sg_set_page(sg, pg[i], seg->length, seg->offset); |
| 564 | pending_req->data_len += seg->length; |
| 565 | seg++; |
| 566 | if (nr_sgl && (unsigned long)seg >= end_seg) { |
| 567 | i_seg++; |
| 568 | end_seg = vaddr(pending_req, i_seg) + |
| 569 | ring_req->seg[i_seg].offset; |
| 570 | seg = (struct scsiif_request_segment *)end_seg; |
| 571 | end_seg += ring_req->seg[i_seg].length; |
| 572 | } |
| 573 | if (sg->offset >= PAGE_SIZE || |
| 574 | sg->length > PAGE_SIZE || |
| 575 | sg->offset + sg->length > PAGE_SIZE) |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static void scsiback_disconnect(struct vscsibk_info *info) |
| 583 | { |
| 584 | wait_event(info->waiting_to_free, |
| 585 | atomic_read(&info->nr_unreplied_reqs) == 0); |
| 586 | |
| 587 | unbind_from_irqhandler(info->irq, info); |
| 588 | info->irq = 0; |
| 589 | xenbus_unmap_ring_vfree(info->dev, info->ring.sring); |
| 590 | } |
| 591 | |
| 592 | static void scsiback_device_action(struct vscsibk_pend *pending_req, |
| 593 | enum tcm_tmreq_table act, int tag) |
| 594 | { |
| 595 | int rc, err = FAILED; |
| 596 | struct scsiback_tpg *tpg = pending_req->v2p->tpg; |
| 597 | struct se_cmd *se_cmd = &pending_req->se_cmd; |
| 598 | struct scsiback_tmr *tmr; |
| 599 | |
| 600 | tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL); |
| 601 | if (!tmr) |
| 602 | goto out; |
| 603 | |
| 604 | init_waitqueue_head(&tmr->tmr_wait); |
| 605 | |
| 606 | transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo, |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 607 | tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG, |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 608 | &pending_req->sense_buffer[0]); |
| 609 | |
| 610 | rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL); |
| 611 | if (rc < 0) |
| 612 | goto out; |
| 613 | |
| 614 | se_cmd->se_tmr_req->ref_task_tag = tag; |
| 615 | |
| 616 | if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0) |
| 617 | goto out; |
| 618 | |
| 619 | transport_generic_handle_tmr(se_cmd); |
| 620 | wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete)); |
| 621 | |
| 622 | err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ? |
| 623 | SUCCESS : FAILED; |
| 624 | |
| 625 | out: |
| 626 | if (tmr) { |
| 627 | transport_generic_free_cmd(&pending_req->se_cmd, 1); |
| 628 | kfree(tmr); |
| 629 | } |
| 630 | |
| 631 | scsiback_do_resp_with_sense(NULL, err, 0, pending_req); |
| 632 | |
| 633 | kmem_cache_free(scsiback_cachep, pending_req); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | Perform virtual to physical translation |
| 638 | */ |
| 639 | static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info, |
| 640 | struct ids_tuple *v) |
| 641 | { |
| 642 | struct v2p_entry *entry; |
| 643 | struct list_head *head = &(info->v2p_entry_lists); |
| 644 | unsigned long flags; |
| 645 | |
| 646 | spin_lock_irqsave(&info->v2p_lock, flags); |
| 647 | list_for_each_entry(entry, head, l) { |
| 648 | if ((entry->v.chn == v->chn) && |
| 649 | (entry->v.tgt == v->tgt) && |
| 650 | (entry->v.lun == v->lun)) { |
| 651 | kref_get(&entry->kref); |
| 652 | goto out; |
| 653 | } |
| 654 | } |
| 655 | entry = NULL; |
| 656 | |
| 657 | out: |
| 658 | spin_unlock_irqrestore(&info->v2p_lock, flags); |
| 659 | return entry; |
| 660 | } |
| 661 | |
| 662 | static int prepare_pending_reqs(struct vscsibk_info *info, |
| 663 | struct vscsiif_request *ring_req, |
| 664 | struct vscsibk_pend *pending_req) |
| 665 | { |
| 666 | struct v2p_entry *v2p; |
| 667 | struct ids_tuple vir; |
| 668 | |
| 669 | pending_req->rqid = ring_req->rqid; |
| 670 | pending_req->info = info; |
| 671 | |
| 672 | vir.chn = ring_req->channel; |
| 673 | vir.tgt = ring_req->id; |
| 674 | vir.lun = ring_req->lun; |
| 675 | |
| 676 | v2p = scsiback_do_translation(info, &vir); |
| 677 | if (!v2p) { |
| 678 | pending_req->v2p = NULL; |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 679 | pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n", |
| 680 | vir.chn, vir.tgt, vir.lun); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 681 | return -ENODEV; |
| 682 | } |
| 683 | pending_req->v2p = v2p; |
| 684 | |
| 685 | /* request range check from frontend */ |
| 686 | pending_req->sc_data_direction = ring_req->sc_data_direction; |
| 687 | if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) && |
| 688 | (pending_req->sc_data_direction != DMA_TO_DEVICE) && |
| 689 | (pending_req->sc_data_direction != DMA_FROM_DEVICE) && |
| 690 | (pending_req->sc_data_direction != DMA_NONE)) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 691 | pr_debug("invalid parameter data_dir = %d\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 692 | pending_req->sc_data_direction); |
| 693 | return -EINVAL; |
| 694 | } |
| 695 | |
| 696 | pending_req->cmd_len = ring_req->cmd_len; |
| 697 | if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 698 | pr_debug("invalid parameter cmd_len = %d\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 699 | pending_req->cmd_len); |
| 700 | return -EINVAL; |
| 701 | } |
| 702 | memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int scsiback_do_cmd_fn(struct vscsibk_info *info) |
| 708 | { |
| 709 | struct vscsiif_back_ring *ring = &info->ring; |
Juergen Gross | facb573 | 2015-02-17 08:02:47 +0100 | [diff] [blame] | 710 | struct vscsiif_request ring_req; |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 711 | struct vscsibk_pend *pending_req; |
| 712 | RING_IDX rc, rp; |
| 713 | int err, more_to_do; |
| 714 | uint32_t result; |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 715 | |
| 716 | rc = ring->req_cons; |
| 717 | rp = ring->sring->req_prod; |
| 718 | rmb(); /* guest system is accessing ring, too */ |
| 719 | |
| 720 | if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) { |
| 721 | rc = ring->rsp_prod_pvt; |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 722 | pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 723 | info->domid, rp, rc, rp - rc); |
| 724 | info->ring_error = 1; |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | while ((rc != rp)) { |
| 729 | if (RING_REQUEST_CONS_OVERFLOW(ring, rc)) |
| 730 | break; |
| 731 | pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL); |
| 732 | if (!pending_req) |
| 733 | return 1; |
| 734 | |
Juergen Gross | facb573 | 2015-02-17 08:02:47 +0100 | [diff] [blame] | 735 | ring_req = *RING_GET_REQUEST(ring, rc); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 736 | ring->req_cons = ++rc; |
| 737 | |
Juergen Gross | facb573 | 2015-02-17 08:02:47 +0100 | [diff] [blame] | 738 | err = prepare_pending_reqs(info, &ring_req, pending_req); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 739 | if (err) { |
| 740 | switch (err) { |
| 741 | case -ENODEV: |
| 742 | result = DID_NO_CONNECT; |
| 743 | break; |
| 744 | default: |
| 745 | result = DRIVER_ERROR; |
| 746 | break; |
| 747 | } |
| 748 | scsiback_do_resp_with_sense(NULL, result << 24, 0, |
| 749 | pending_req); |
| 750 | kmem_cache_free(scsiback_cachep, pending_req); |
| 751 | return 1; |
| 752 | } |
| 753 | |
Juergen Gross | facb573 | 2015-02-17 08:02:47 +0100 | [diff] [blame] | 754 | switch (ring_req.act) { |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 755 | case VSCSIIF_ACT_SCSI_CDB: |
Juergen Gross | facb573 | 2015-02-17 08:02:47 +0100 | [diff] [blame] | 756 | if (scsiback_gnttab_data_map(&ring_req, pending_req)) { |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 757 | scsiback_fast_flush_area(pending_req); |
| 758 | scsiback_do_resp_with_sense(NULL, |
| 759 | DRIVER_ERROR << 24, 0, pending_req); |
| 760 | kmem_cache_free(scsiback_cachep, pending_req); |
| 761 | } else { |
| 762 | scsiback_cmd_exec(pending_req); |
| 763 | } |
| 764 | break; |
| 765 | case VSCSIIF_ACT_SCSI_ABORT: |
| 766 | scsiback_device_action(pending_req, TMR_ABORT_TASK, |
Juergen Gross | facb573 | 2015-02-17 08:02:47 +0100 | [diff] [blame] | 767 | ring_req.ref_rqid); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 768 | break; |
| 769 | case VSCSIIF_ACT_SCSI_RESET: |
| 770 | scsiback_device_action(pending_req, TMR_LUN_RESET, 0); |
| 771 | break; |
| 772 | default: |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 773 | pr_err_ratelimited("invalid request\n"); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 774 | scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, |
| 775 | 0, pending_req); |
| 776 | kmem_cache_free(scsiback_cachep, pending_req); |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | /* Yield point for this unbounded loop. */ |
| 781 | cond_resched(); |
| 782 | } |
| 783 | |
| 784 | RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do); |
| 785 | return more_to_do; |
| 786 | } |
| 787 | |
| 788 | static irqreturn_t scsiback_irq_fn(int irq, void *dev_id) |
| 789 | { |
| 790 | struct vscsibk_info *info = dev_id; |
| 791 | |
| 792 | if (info->ring_error) |
| 793 | return IRQ_HANDLED; |
| 794 | |
| 795 | while (scsiback_do_cmd_fn(info)) |
| 796 | cond_resched(); |
| 797 | |
| 798 | return IRQ_HANDLED; |
| 799 | } |
| 800 | |
| 801 | static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref, |
| 802 | evtchn_port_t evtchn) |
| 803 | { |
| 804 | void *area; |
| 805 | struct vscsiif_sring *sring; |
| 806 | int err; |
| 807 | |
| 808 | if (info->irq) |
| 809 | return -1; |
| 810 | |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 811 | err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 812 | if (err) |
| 813 | return err; |
| 814 | |
| 815 | sring = (struct vscsiif_sring *)area; |
| 816 | BACK_RING_INIT(&info->ring, sring, PAGE_SIZE); |
| 817 | |
| 818 | err = bind_interdomain_evtchn_to_irq(info->domid, evtchn); |
| 819 | if (err < 0) |
| 820 | goto unmap_page; |
| 821 | |
| 822 | info->irq = err; |
| 823 | |
| 824 | err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn, |
| 825 | IRQF_ONESHOT, "vscsiif-backend", info); |
| 826 | if (err) |
| 827 | goto free_irq; |
| 828 | |
| 829 | return 0; |
| 830 | |
| 831 | free_irq: |
| 832 | unbind_from_irqhandler(info->irq, info); |
| 833 | info->irq = 0; |
| 834 | unmap_page: |
| 835 | xenbus_unmap_ring_vfree(info->dev, area); |
| 836 | |
| 837 | return err; |
| 838 | } |
| 839 | |
| 840 | static int scsiback_map(struct vscsibk_info *info) |
| 841 | { |
| 842 | struct xenbus_device *dev = info->dev; |
| 843 | unsigned int ring_ref, evtchn; |
| 844 | int err; |
| 845 | |
| 846 | err = xenbus_gather(XBT_NIL, dev->otherend, |
| 847 | "ring-ref", "%u", &ring_ref, |
| 848 | "event-channel", "%u", &evtchn, NULL); |
| 849 | if (err) { |
| 850 | xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend); |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | return scsiback_init_sring(info, ring_ref, evtchn); |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | Add a new translation entry |
| 859 | */ |
| 860 | static int scsiback_add_translation_entry(struct vscsibk_info *info, |
| 861 | char *phy, struct ids_tuple *v) |
| 862 | { |
| 863 | int err = 0; |
| 864 | struct v2p_entry *entry; |
| 865 | struct v2p_entry *new; |
| 866 | struct list_head *head = &(info->v2p_entry_lists); |
| 867 | unsigned long flags; |
| 868 | char *lunp; |
| 869 | unsigned int lun; |
| 870 | struct scsiback_tpg *tpg_entry, *tpg = NULL; |
| 871 | char *error = "doesn't exist"; |
| 872 | |
| 873 | lunp = strrchr(phy, ':'); |
| 874 | if (!lunp) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 875 | pr_err("illegal format of physical device %s\n", phy); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 876 | return -EINVAL; |
| 877 | } |
| 878 | *lunp = 0; |
| 879 | lunp++; |
| 880 | if (kstrtouint(lunp, 10, &lun) || lun >= TRANSPORT_MAX_LUNS_PER_TPG) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 881 | pr_err("lun number not valid: %s\n", lunp); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | } |
| 884 | |
| 885 | mutex_lock(&scsiback_mutex); |
| 886 | list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) { |
| 887 | if (!strcmp(phy, tpg_entry->tport->tport_name) || |
| 888 | !strcmp(phy, tpg_entry->param_alias)) { |
| 889 | spin_lock(&tpg_entry->se_tpg.tpg_lun_lock); |
| 890 | if (tpg_entry->se_tpg.tpg_lun_list[lun]->lun_status == |
| 891 | TRANSPORT_LUN_STATUS_ACTIVE) { |
| 892 | if (!tpg_entry->tpg_nexus) |
| 893 | error = "nexus undefined"; |
| 894 | else |
| 895 | tpg = tpg_entry; |
| 896 | } |
| 897 | spin_unlock(&tpg_entry->se_tpg.tpg_lun_lock); |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | if (tpg) { |
| 902 | mutex_lock(&tpg->tv_tpg_mutex); |
| 903 | tpg->tv_tpg_fe_count++; |
| 904 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 905 | } |
| 906 | mutex_unlock(&scsiback_mutex); |
| 907 | |
| 908 | if (!tpg) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 909 | pr_err("%s:%d %s\n", phy, lun, error); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 910 | return -ENODEV; |
| 911 | } |
| 912 | |
| 913 | new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL); |
| 914 | if (new == NULL) { |
| 915 | err = -ENOMEM; |
| 916 | goto out_free; |
| 917 | } |
| 918 | |
| 919 | spin_lock_irqsave(&info->v2p_lock, flags); |
| 920 | |
| 921 | /* Check double assignment to identical virtual ID */ |
| 922 | list_for_each_entry(entry, head, l) { |
| 923 | if ((entry->v.chn == v->chn) && |
| 924 | (entry->v.tgt == v->tgt) && |
| 925 | (entry->v.lun == v->lun)) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 926 | pr_warn("Virtual ID is already used. Assignment was not performed.\n"); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 927 | err = -EEXIST; |
| 928 | goto out; |
| 929 | } |
| 930 | |
| 931 | } |
| 932 | |
| 933 | /* Create a new translation entry and add to the list */ |
| 934 | kref_init(&new->kref); |
| 935 | new->v = *v; |
| 936 | new->tpg = tpg; |
| 937 | new->lun = lun; |
| 938 | list_add_tail(&new->l, head); |
| 939 | |
| 940 | out: |
| 941 | spin_unlock_irqrestore(&info->v2p_lock, flags); |
| 942 | |
| 943 | out_free: |
| 944 | mutex_lock(&tpg->tv_tpg_mutex); |
| 945 | tpg->tv_tpg_fe_count--; |
| 946 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 947 | |
| 948 | if (err) |
| 949 | kfree(new); |
| 950 | |
| 951 | return err; |
| 952 | } |
| 953 | |
| 954 | static void __scsiback_del_translation_entry(struct v2p_entry *entry) |
| 955 | { |
| 956 | list_del(&entry->l); |
| 957 | kref_put(&entry->kref, scsiback_free_translation_entry); |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | Delete the translation entry specfied |
| 962 | */ |
| 963 | static int scsiback_del_translation_entry(struct vscsibk_info *info, |
| 964 | struct ids_tuple *v) |
| 965 | { |
| 966 | struct v2p_entry *entry; |
| 967 | struct list_head *head = &(info->v2p_entry_lists); |
| 968 | unsigned long flags; |
| 969 | |
| 970 | spin_lock_irqsave(&info->v2p_lock, flags); |
| 971 | /* Find out the translation entry specified */ |
| 972 | list_for_each_entry(entry, head, l) { |
| 973 | if ((entry->v.chn == v->chn) && |
| 974 | (entry->v.tgt == v->tgt) && |
| 975 | (entry->v.lun == v->lun)) { |
| 976 | goto found; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | spin_unlock_irqrestore(&info->v2p_lock, flags); |
| 981 | return 1; |
| 982 | |
| 983 | found: |
| 984 | /* Delete the translation entry specfied */ |
| 985 | __scsiback_del_translation_entry(entry); |
| 986 | |
| 987 | spin_unlock_irqrestore(&info->v2p_lock, flags); |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state, |
Juergen Gross | 169e6cf | 2015-02-17 08:02:48 +0100 | [diff] [blame] | 992 | char *phy, struct ids_tuple *vir, int try) |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 993 | { |
| 994 | if (!scsiback_add_translation_entry(info, phy, vir)) { |
| 995 | if (xenbus_printf(XBT_NIL, info->dev->nodename, state, |
| 996 | "%d", XenbusStateInitialised)) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 997 | pr_err("xenbus_printf error %s\n", state); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 998 | scsiback_del_translation_entry(info, vir); |
| 999 | } |
Juergen Gross | 169e6cf | 2015-02-17 08:02:48 +0100 | [diff] [blame] | 1000 | } else if (!try) { |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1001 | xenbus_printf(XBT_NIL, info->dev->nodename, state, |
| 1002 | "%d", XenbusStateClosed); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state, |
| 1007 | struct ids_tuple *vir) |
| 1008 | { |
| 1009 | if (!scsiback_del_translation_entry(info, vir)) { |
| 1010 | if (xenbus_printf(XBT_NIL, info->dev->nodename, state, |
| 1011 | "%d", XenbusStateClosed)) |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1012 | pr_err("xenbus_printf error %s\n", state); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1 |
| 1017 | #define VSCSIBACK_OP_UPDATEDEV_STATE 2 |
| 1018 | |
| 1019 | static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op, |
| 1020 | char *ent) |
| 1021 | { |
| 1022 | int err; |
| 1023 | struct ids_tuple vir; |
| 1024 | char *val; |
| 1025 | int device_state; |
| 1026 | char phy[VSCSI_NAMELEN]; |
| 1027 | char str[64]; |
| 1028 | char state[64]; |
| 1029 | struct xenbus_device *dev = info->dev; |
| 1030 | |
| 1031 | /* read status */ |
| 1032 | snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent); |
| 1033 | err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state); |
| 1034 | if (XENBUS_EXIST_ERR(err)) |
| 1035 | return; |
| 1036 | |
| 1037 | /* physical SCSI device */ |
| 1038 | snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent); |
| 1039 | val = xenbus_read(XBT_NIL, dev->nodename, str, NULL); |
| 1040 | if (IS_ERR(val)) { |
| 1041 | xenbus_printf(XBT_NIL, dev->nodename, state, |
| 1042 | "%d", XenbusStateClosed); |
| 1043 | return; |
| 1044 | } |
| 1045 | strlcpy(phy, val, VSCSI_NAMELEN); |
| 1046 | kfree(val); |
| 1047 | |
| 1048 | /* virtual SCSI device */ |
| 1049 | snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent); |
| 1050 | err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u", |
| 1051 | &vir.hst, &vir.chn, &vir.tgt, &vir.lun); |
| 1052 | if (XENBUS_EXIST_ERR(err)) { |
| 1053 | xenbus_printf(XBT_NIL, dev->nodename, state, |
| 1054 | "%d", XenbusStateClosed); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | switch (op) { |
| 1059 | case VSCSIBACK_OP_ADD_OR_DEL_LUN: |
Juergen Gross | 169e6cf | 2015-02-17 08:02:48 +0100 | [diff] [blame] | 1060 | switch (device_state) { |
| 1061 | case XenbusStateInitialising: |
| 1062 | scsiback_do_add_lun(info, state, phy, &vir, 0); |
| 1063 | break; |
| 1064 | case XenbusStateConnected: |
| 1065 | scsiback_do_add_lun(info, state, phy, &vir, 1); |
| 1066 | break; |
| 1067 | case XenbusStateClosing: |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1068 | scsiback_do_del_lun(info, state, &vir); |
Juergen Gross | 169e6cf | 2015-02-17 08:02:48 +0100 | [diff] [blame] | 1069 | break; |
| 1070 | default: |
| 1071 | break; |
| 1072 | } |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1073 | break; |
| 1074 | |
| 1075 | case VSCSIBACK_OP_UPDATEDEV_STATE: |
| 1076 | if (device_state == XenbusStateInitialised) { |
| 1077 | /* modify vscsi-devs/dev-x/state */ |
| 1078 | if (xenbus_printf(XBT_NIL, dev->nodename, state, |
| 1079 | "%d", XenbusStateConnected)) { |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1080 | pr_err("xenbus_printf error %s\n", str); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1081 | scsiback_del_translation_entry(info, &vir); |
| 1082 | xenbus_printf(XBT_NIL, dev->nodename, state, |
| 1083 | "%d", XenbusStateClosed); |
| 1084 | } |
| 1085 | } |
| 1086 | break; |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1087 | /* When it is necessary, processing is added here. */ |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1088 | default: |
| 1089 | break; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op) |
| 1094 | { |
| 1095 | int i; |
| 1096 | char **dir; |
| 1097 | unsigned int ndir = 0; |
| 1098 | |
| 1099 | dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs", |
| 1100 | &ndir); |
| 1101 | if (IS_ERR(dir)) |
| 1102 | return; |
| 1103 | |
| 1104 | for (i = 0; i < ndir; i++) |
| 1105 | scsiback_do_1lun_hotplug(info, op, dir[i]); |
| 1106 | |
| 1107 | kfree(dir); |
| 1108 | } |
| 1109 | |
| 1110 | static void scsiback_frontend_changed(struct xenbus_device *dev, |
| 1111 | enum xenbus_state frontend_state) |
| 1112 | { |
| 1113 | struct vscsibk_info *info = dev_get_drvdata(&dev->dev); |
| 1114 | |
| 1115 | switch (frontend_state) { |
| 1116 | case XenbusStateInitialising: |
| 1117 | break; |
| 1118 | |
| 1119 | case XenbusStateInitialised: |
| 1120 | if (scsiback_map(info)) |
| 1121 | break; |
| 1122 | |
| 1123 | scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN); |
| 1124 | xenbus_switch_state(dev, XenbusStateConnected); |
| 1125 | break; |
| 1126 | |
| 1127 | case XenbusStateConnected: |
| 1128 | scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE); |
| 1129 | |
| 1130 | if (dev->state == XenbusStateConnected) |
| 1131 | break; |
| 1132 | |
| 1133 | xenbus_switch_state(dev, XenbusStateConnected); |
| 1134 | break; |
| 1135 | |
| 1136 | case XenbusStateClosing: |
| 1137 | if (info->irq) |
| 1138 | scsiback_disconnect(info); |
| 1139 | |
| 1140 | xenbus_switch_state(dev, XenbusStateClosing); |
| 1141 | break; |
| 1142 | |
| 1143 | case XenbusStateClosed: |
| 1144 | xenbus_switch_state(dev, XenbusStateClosed); |
| 1145 | if (xenbus_dev_is_online(dev)) |
| 1146 | break; |
| 1147 | /* fall through if not online */ |
| 1148 | case XenbusStateUnknown: |
| 1149 | device_unregister(&dev->dev); |
| 1150 | break; |
| 1151 | |
| 1152 | case XenbusStateReconfiguring: |
| 1153 | scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN); |
| 1154 | xenbus_switch_state(dev, XenbusStateReconfigured); |
| 1155 | |
| 1156 | break; |
| 1157 | |
| 1158 | default: |
| 1159 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", |
| 1160 | frontend_state); |
| 1161 | break; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | Release the translation entry specfied |
| 1167 | */ |
| 1168 | static void scsiback_release_translation_entry(struct vscsibk_info *info) |
| 1169 | { |
| 1170 | struct v2p_entry *entry, *tmp; |
| 1171 | struct list_head *head = &(info->v2p_entry_lists); |
| 1172 | unsigned long flags; |
| 1173 | |
| 1174 | spin_lock_irqsave(&info->v2p_lock, flags); |
| 1175 | |
| 1176 | list_for_each_entry_safe(entry, tmp, head, l) |
| 1177 | __scsiback_del_translation_entry(entry); |
| 1178 | |
| 1179 | spin_unlock_irqrestore(&info->v2p_lock, flags); |
| 1180 | } |
| 1181 | |
| 1182 | static int scsiback_remove(struct xenbus_device *dev) |
| 1183 | { |
| 1184 | struct vscsibk_info *info = dev_get_drvdata(&dev->dev); |
| 1185 | |
| 1186 | if (info->irq) |
| 1187 | scsiback_disconnect(info); |
| 1188 | |
| 1189 | scsiback_release_translation_entry(info); |
| 1190 | |
| 1191 | dev_set_drvdata(&dev->dev, NULL); |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static int scsiback_probe(struct xenbus_device *dev, |
| 1197 | const struct xenbus_device_id *id) |
| 1198 | { |
| 1199 | int err; |
| 1200 | |
| 1201 | struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info), |
| 1202 | GFP_KERNEL); |
| 1203 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1204 | pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1205 | |
| 1206 | if (!info) { |
| 1207 | xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure"); |
| 1208 | return -ENOMEM; |
| 1209 | } |
| 1210 | info->dev = dev; |
| 1211 | dev_set_drvdata(&dev->dev, info); |
| 1212 | |
| 1213 | info->domid = dev->otherend_id; |
| 1214 | spin_lock_init(&info->ring_lock); |
| 1215 | info->ring_error = 0; |
| 1216 | atomic_set(&info->nr_unreplied_reqs, 0); |
| 1217 | init_waitqueue_head(&info->waiting_to_free); |
| 1218 | info->dev = dev; |
| 1219 | info->irq = 0; |
| 1220 | INIT_LIST_HEAD(&info->v2p_entry_lists); |
| 1221 | spin_lock_init(&info->v2p_lock); |
| 1222 | |
| 1223 | err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u", |
| 1224 | SG_ALL); |
| 1225 | if (err) |
| 1226 | xenbus_dev_error(dev, err, "writing feature-sg-grant"); |
| 1227 | |
| 1228 | err = xenbus_switch_state(dev, XenbusStateInitWait); |
| 1229 | if (err) |
| 1230 | goto fail; |
| 1231 | |
| 1232 | return 0; |
| 1233 | |
| 1234 | fail: |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1235 | pr_warn("%s failed\n", __func__); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1236 | scsiback_remove(dev); |
| 1237 | |
| 1238 | return err; |
| 1239 | } |
| 1240 | |
| 1241 | static char *scsiback_dump_proto_id(struct scsiback_tport *tport) |
| 1242 | { |
| 1243 | switch (tport->tport_proto_id) { |
| 1244 | case SCSI_PROTOCOL_SAS: |
| 1245 | return "SAS"; |
| 1246 | case SCSI_PROTOCOL_FCP: |
| 1247 | return "FCP"; |
| 1248 | case SCSI_PROTOCOL_ISCSI: |
| 1249 | return "iSCSI"; |
| 1250 | default: |
| 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | return "Unknown"; |
| 1255 | } |
| 1256 | |
| 1257 | static u8 scsiback_get_fabric_proto_ident(struct se_portal_group *se_tpg) |
| 1258 | { |
| 1259 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1260 | struct scsiback_tpg, se_tpg); |
| 1261 | struct scsiback_tport *tport = tpg->tport; |
| 1262 | |
| 1263 | switch (tport->tport_proto_id) { |
| 1264 | case SCSI_PROTOCOL_SAS: |
| 1265 | return sas_get_fabric_proto_ident(se_tpg); |
| 1266 | case SCSI_PROTOCOL_FCP: |
| 1267 | return fc_get_fabric_proto_ident(se_tpg); |
| 1268 | case SCSI_PROTOCOL_ISCSI: |
| 1269 | return iscsi_get_fabric_proto_ident(se_tpg); |
| 1270 | default: |
| 1271 | pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", |
| 1272 | tport->tport_proto_id); |
| 1273 | break; |
| 1274 | } |
| 1275 | |
| 1276 | return sas_get_fabric_proto_ident(se_tpg); |
| 1277 | } |
| 1278 | |
| 1279 | static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg) |
| 1280 | { |
| 1281 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1282 | struct scsiback_tpg, se_tpg); |
| 1283 | struct scsiback_tport *tport = tpg->tport; |
| 1284 | |
| 1285 | return &tport->tport_name[0]; |
| 1286 | } |
| 1287 | |
| 1288 | static u16 scsiback_get_tag(struct se_portal_group *se_tpg) |
| 1289 | { |
| 1290 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1291 | struct scsiback_tpg, se_tpg); |
| 1292 | return tpg->tport_tpgt; |
| 1293 | } |
| 1294 | |
| 1295 | static u32 scsiback_get_default_depth(struct se_portal_group *se_tpg) |
| 1296 | { |
| 1297 | return 1; |
| 1298 | } |
| 1299 | |
| 1300 | static u32 |
| 1301 | scsiback_get_pr_transport_id(struct se_portal_group *se_tpg, |
| 1302 | struct se_node_acl *se_nacl, |
| 1303 | struct t10_pr_registration *pr_reg, |
| 1304 | int *format_code, |
| 1305 | unsigned char *buf) |
| 1306 | { |
| 1307 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1308 | struct scsiback_tpg, se_tpg); |
| 1309 | struct scsiback_tport *tport = tpg->tport; |
| 1310 | |
| 1311 | switch (tport->tport_proto_id) { |
| 1312 | case SCSI_PROTOCOL_SAS: |
| 1313 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 1314 | format_code, buf); |
| 1315 | case SCSI_PROTOCOL_FCP: |
| 1316 | return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 1317 | format_code, buf); |
| 1318 | case SCSI_PROTOCOL_ISCSI: |
| 1319 | return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 1320 | format_code, buf); |
| 1321 | default: |
| 1322 | pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", |
| 1323 | tport->tport_proto_id); |
| 1324 | break; |
| 1325 | } |
| 1326 | |
| 1327 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 1328 | format_code, buf); |
| 1329 | } |
| 1330 | |
| 1331 | static u32 |
| 1332 | scsiback_get_pr_transport_id_len(struct se_portal_group *se_tpg, |
| 1333 | struct se_node_acl *se_nacl, |
| 1334 | struct t10_pr_registration *pr_reg, |
| 1335 | int *format_code) |
| 1336 | { |
| 1337 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1338 | struct scsiback_tpg, se_tpg); |
| 1339 | struct scsiback_tport *tport = tpg->tport; |
| 1340 | |
| 1341 | switch (tport->tport_proto_id) { |
| 1342 | case SCSI_PROTOCOL_SAS: |
| 1343 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 1344 | format_code); |
| 1345 | case SCSI_PROTOCOL_FCP: |
| 1346 | return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 1347 | format_code); |
| 1348 | case SCSI_PROTOCOL_ISCSI: |
| 1349 | return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 1350 | format_code); |
| 1351 | default: |
| 1352 | pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", |
| 1353 | tport->tport_proto_id); |
| 1354 | break; |
| 1355 | } |
| 1356 | |
| 1357 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 1358 | format_code); |
| 1359 | } |
| 1360 | |
| 1361 | static char * |
| 1362 | scsiback_parse_pr_out_transport_id(struct se_portal_group *se_tpg, |
| 1363 | const char *buf, |
| 1364 | u32 *out_tid_len, |
| 1365 | char **port_nexus_ptr) |
| 1366 | { |
| 1367 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1368 | struct scsiback_tpg, se_tpg); |
| 1369 | struct scsiback_tport *tport = tpg->tport; |
| 1370 | |
| 1371 | switch (tport->tport_proto_id) { |
| 1372 | case SCSI_PROTOCOL_SAS: |
| 1373 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 1374 | port_nexus_ptr); |
| 1375 | case SCSI_PROTOCOL_FCP: |
| 1376 | return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 1377 | port_nexus_ptr); |
| 1378 | case SCSI_PROTOCOL_ISCSI: |
| 1379 | return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 1380 | port_nexus_ptr); |
| 1381 | default: |
| 1382 | pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n", |
| 1383 | tport->tport_proto_id); |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 1388 | port_nexus_ptr); |
| 1389 | } |
| 1390 | |
| 1391 | static struct se_wwn * |
| 1392 | scsiback_make_tport(struct target_fabric_configfs *tf, |
| 1393 | struct config_group *group, |
| 1394 | const char *name) |
| 1395 | { |
| 1396 | struct scsiback_tport *tport; |
| 1397 | char *ptr; |
| 1398 | u64 wwpn = 0; |
| 1399 | int off = 0; |
| 1400 | |
| 1401 | tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL); |
| 1402 | if (!tport) |
| 1403 | return ERR_PTR(-ENOMEM); |
| 1404 | |
| 1405 | tport->tport_wwpn = wwpn; |
| 1406 | /* |
| 1407 | * Determine the emulated Protocol Identifier and Target Port Name |
| 1408 | * based on the incoming configfs directory name. |
| 1409 | */ |
| 1410 | ptr = strstr(name, "naa."); |
| 1411 | if (ptr) { |
| 1412 | tport->tport_proto_id = SCSI_PROTOCOL_SAS; |
| 1413 | goto check_len; |
| 1414 | } |
| 1415 | ptr = strstr(name, "fc."); |
| 1416 | if (ptr) { |
| 1417 | tport->tport_proto_id = SCSI_PROTOCOL_FCP; |
| 1418 | off = 3; /* Skip over "fc." */ |
| 1419 | goto check_len; |
| 1420 | } |
| 1421 | ptr = strstr(name, "iqn."); |
| 1422 | if (ptr) { |
| 1423 | tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; |
| 1424 | goto check_len; |
| 1425 | } |
| 1426 | |
| 1427 | pr_err("Unable to locate prefix for emulated Target Port: %s\n", name); |
| 1428 | kfree(tport); |
| 1429 | return ERR_PTR(-EINVAL); |
| 1430 | |
| 1431 | check_len: |
| 1432 | if (strlen(name) >= VSCSI_NAMELEN) { |
| 1433 | pr_err("Emulated %s Address: %s, exceeds max: %d\n", name, |
| 1434 | scsiback_dump_proto_id(tport), VSCSI_NAMELEN); |
| 1435 | kfree(tport); |
| 1436 | return ERR_PTR(-EINVAL); |
| 1437 | } |
| 1438 | snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]); |
| 1439 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1440 | pr_debug("Allocated emulated Target %s Address: %s\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1441 | scsiback_dump_proto_id(tport), name); |
| 1442 | |
| 1443 | return &tport->tport_wwn; |
| 1444 | } |
| 1445 | |
| 1446 | static void scsiback_drop_tport(struct se_wwn *wwn) |
| 1447 | { |
| 1448 | struct scsiback_tport *tport = container_of(wwn, |
| 1449 | struct scsiback_tport, tport_wwn); |
| 1450 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1451 | pr_debug("Deallocating emulated Target %s Address: %s\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1452 | scsiback_dump_proto_id(tport), tport->tport_name); |
| 1453 | |
| 1454 | kfree(tport); |
| 1455 | } |
| 1456 | |
| 1457 | static struct se_node_acl * |
| 1458 | scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg) |
| 1459 | { |
| 1460 | return kzalloc(sizeof(struct se_node_acl), GFP_KERNEL); |
| 1461 | } |
| 1462 | |
| 1463 | static void |
| 1464 | scsiback_release_fabric_acl(struct se_portal_group *se_tpg, |
| 1465 | struct se_node_acl *se_nacl) |
| 1466 | { |
| 1467 | kfree(se_nacl); |
| 1468 | } |
| 1469 | |
| 1470 | static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg) |
| 1471 | { |
| 1472 | return 1; |
| 1473 | } |
| 1474 | |
| 1475 | static int scsiback_check_stop_free(struct se_cmd *se_cmd) |
| 1476 | { |
| 1477 | /* |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1478 | * Do not release struct se_cmd's containing a valid TMR pointer. |
| 1479 | * These will be released directly in scsiback_device_action() |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1480 | * with transport_generic_free_cmd(). |
| 1481 | */ |
| 1482 | if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
| 1483 | return 0; |
| 1484 | |
| 1485 | transport_generic_free_cmd(se_cmd, 0); |
| 1486 | return 1; |
| 1487 | } |
| 1488 | |
| 1489 | static void scsiback_release_cmd(struct se_cmd *se_cmd) |
| 1490 | { |
| 1491 | struct vscsibk_pend *pending_req = container_of(se_cmd, |
| 1492 | struct vscsibk_pend, se_cmd); |
| 1493 | |
| 1494 | kmem_cache_free(scsiback_cachep, pending_req); |
| 1495 | } |
| 1496 | |
| 1497 | static int scsiback_shutdown_session(struct se_session *se_sess) |
| 1498 | { |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | static void scsiback_close_session(struct se_session *se_sess) |
| 1503 | { |
| 1504 | } |
| 1505 | |
| 1506 | static u32 scsiback_sess_get_index(struct se_session *se_sess) |
| 1507 | { |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | static int scsiback_write_pending(struct se_cmd *se_cmd) |
| 1512 | { |
| 1513 | /* Go ahead and process the write immediately */ |
| 1514 | target_execute_cmd(se_cmd); |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static int scsiback_write_pending_status(struct se_cmd *se_cmd) |
| 1520 | { |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | static void scsiback_set_default_node_attrs(struct se_node_acl *nacl) |
| 1525 | { |
| 1526 | } |
| 1527 | |
| 1528 | static u32 scsiback_get_task_tag(struct se_cmd *se_cmd) |
| 1529 | { |
| 1530 | struct vscsibk_pend *pending_req = container_of(se_cmd, |
| 1531 | struct vscsibk_pend, se_cmd); |
| 1532 | |
| 1533 | return pending_req->rqid; |
| 1534 | } |
| 1535 | |
| 1536 | static int scsiback_get_cmd_state(struct se_cmd *se_cmd) |
| 1537 | { |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static int scsiback_queue_data_in(struct se_cmd *se_cmd) |
| 1542 | { |
| 1543 | struct vscsibk_pend *pending_req = container_of(se_cmd, |
| 1544 | struct vscsibk_pend, se_cmd); |
| 1545 | |
| 1546 | pending_req->result = SAM_STAT_GOOD; |
| 1547 | scsiback_cmd_done(pending_req); |
| 1548 | return 0; |
| 1549 | } |
| 1550 | |
| 1551 | static int scsiback_queue_status(struct se_cmd *se_cmd) |
| 1552 | { |
| 1553 | struct vscsibk_pend *pending_req = container_of(se_cmd, |
| 1554 | struct vscsibk_pend, se_cmd); |
| 1555 | |
| 1556 | if (se_cmd->sense_buffer && |
| 1557 | ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || |
| 1558 | (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) |
| 1559 | pending_req->result = (DRIVER_SENSE << 24) | |
| 1560 | SAM_STAT_CHECK_CONDITION; |
| 1561 | else |
| 1562 | pending_req->result = se_cmd->scsi_status; |
| 1563 | |
| 1564 | scsiback_cmd_done(pending_req); |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
| 1568 | static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd) |
| 1569 | { |
| 1570 | struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; |
| 1571 | struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr; |
| 1572 | |
| 1573 | atomic_set(&tmr->tmr_complete, 1); |
| 1574 | wake_up(&tmr->tmr_wait); |
| 1575 | } |
| 1576 | |
| 1577 | static void scsiback_aborted_task(struct se_cmd *se_cmd) |
| 1578 | { |
| 1579 | } |
| 1580 | |
| 1581 | static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg, |
| 1582 | char *page) |
| 1583 | { |
| 1584 | struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg, |
| 1585 | se_tpg); |
| 1586 | ssize_t rb; |
| 1587 | |
| 1588 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1589 | rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias); |
| 1590 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1591 | |
| 1592 | return rb; |
| 1593 | } |
| 1594 | |
| 1595 | static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg, |
| 1596 | const char *page, size_t count) |
| 1597 | { |
| 1598 | struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg, |
| 1599 | se_tpg); |
| 1600 | int len; |
| 1601 | |
| 1602 | if (strlen(page) >= VSCSI_NAMELEN) { |
| 1603 | pr_err("param alias: %s, exceeds max: %d\n", page, |
| 1604 | VSCSI_NAMELEN); |
| 1605 | return -EINVAL; |
| 1606 | } |
| 1607 | |
| 1608 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1609 | len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page); |
| 1610 | if (tpg->param_alias[len - 1] == '\n') |
| 1611 | tpg->param_alias[len - 1] = '\0'; |
| 1612 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1613 | |
| 1614 | return count; |
| 1615 | } |
| 1616 | |
| 1617 | TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR); |
| 1618 | |
| 1619 | static struct configfs_attribute *scsiback_param_attrs[] = { |
| 1620 | &scsiback_tpg_param_alias.attr, |
| 1621 | NULL, |
| 1622 | }; |
| 1623 | |
| 1624 | static int scsiback_make_nexus(struct scsiback_tpg *tpg, |
| 1625 | const char *name) |
| 1626 | { |
| 1627 | struct se_portal_group *se_tpg; |
| 1628 | struct se_session *se_sess; |
| 1629 | struct scsiback_nexus *tv_nexus; |
| 1630 | |
| 1631 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1632 | if (tpg->tpg_nexus) { |
| 1633 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1634 | pr_debug("tpg->tpg_nexus already exists\n"); |
| 1635 | return -EEXIST; |
| 1636 | } |
| 1637 | se_tpg = &tpg->se_tpg; |
| 1638 | |
| 1639 | tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL); |
| 1640 | if (!tv_nexus) { |
| 1641 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1642 | return -ENOMEM; |
| 1643 | } |
| 1644 | /* |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1645 | * Initialize the struct se_session pointer |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1646 | */ |
| 1647 | tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL); |
| 1648 | if (IS_ERR(tv_nexus->tvn_se_sess)) { |
| 1649 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1650 | kfree(tv_nexus); |
| 1651 | return -ENOMEM; |
| 1652 | } |
| 1653 | se_sess = tv_nexus->tvn_se_sess; |
| 1654 | /* |
| 1655 | * Since we are running in 'demo mode' this call with generate a |
| 1656 | * struct se_node_acl for the scsiback struct se_portal_group with |
| 1657 | * the SCSI Initiator port name of the passed configfs group 'name'. |
| 1658 | */ |
| 1659 | tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( |
| 1660 | se_tpg, (unsigned char *)name); |
| 1661 | if (!tv_nexus->tvn_se_sess->se_node_acl) { |
| 1662 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1663 | pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n", |
| 1664 | name); |
| 1665 | goto out; |
| 1666 | } |
Bart Van Assche | 2f450cc | 2015-02-12 11:48:49 +0100 | [diff] [blame] | 1667 | /* Now register the TCM pvscsi virtual I_T Nexus as active. */ |
| 1668 | transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1669 | tv_nexus->tvn_se_sess, tv_nexus); |
| 1670 | tpg->tpg_nexus = tv_nexus; |
| 1671 | |
| 1672 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1673 | return 0; |
| 1674 | |
| 1675 | out: |
| 1676 | transport_free_session(se_sess); |
| 1677 | kfree(tv_nexus); |
| 1678 | return -ENOMEM; |
| 1679 | } |
| 1680 | |
| 1681 | static int scsiback_drop_nexus(struct scsiback_tpg *tpg) |
| 1682 | { |
| 1683 | struct se_session *se_sess; |
| 1684 | struct scsiback_nexus *tv_nexus; |
| 1685 | |
| 1686 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1687 | tv_nexus = tpg->tpg_nexus; |
| 1688 | if (!tv_nexus) { |
| 1689 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1690 | return -ENODEV; |
| 1691 | } |
| 1692 | |
| 1693 | se_sess = tv_nexus->tvn_se_sess; |
| 1694 | if (!se_sess) { |
| 1695 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1696 | return -ENODEV; |
| 1697 | } |
| 1698 | |
| 1699 | if (tpg->tv_tpg_port_count != 0) { |
| 1700 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1701 | pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n", |
| 1702 | tpg->tv_tpg_port_count); |
| 1703 | return -EBUSY; |
| 1704 | } |
| 1705 | |
| 1706 | if (tpg->tv_tpg_fe_count != 0) { |
| 1707 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1708 | pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n", |
| 1709 | tpg->tv_tpg_fe_count); |
| 1710 | return -EBUSY; |
| 1711 | } |
| 1712 | |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1713 | pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1714 | scsiback_dump_proto_id(tpg->tport), |
| 1715 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); |
| 1716 | |
| 1717 | /* |
| 1718 | * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port |
| 1719 | */ |
| 1720 | transport_deregister_session(tv_nexus->tvn_se_sess); |
| 1721 | tpg->tpg_nexus = NULL; |
| 1722 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1723 | |
| 1724 | kfree(tv_nexus); |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg, |
| 1729 | char *page) |
| 1730 | { |
| 1731 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1732 | struct scsiback_tpg, se_tpg); |
| 1733 | struct scsiback_nexus *tv_nexus; |
| 1734 | ssize_t ret; |
| 1735 | |
| 1736 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1737 | tv_nexus = tpg->tpg_nexus; |
| 1738 | if (!tv_nexus) { |
| 1739 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1740 | return -ENODEV; |
| 1741 | } |
| 1742 | ret = snprintf(page, PAGE_SIZE, "%s\n", |
| 1743 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); |
| 1744 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1745 | |
| 1746 | return ret; |
| 1747 | } |
| 1748 | |
| 1749 | static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg, |
| 1750 | const char *page, |
| 1751 | size_t count) |
| 1752 | { |
| 1753 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1754 | struct scsiback_tpg, se_tpg); |
| 1755 | struct scsiback_tport *tport_wwn = tpg->tport; |
| 1756 | unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr; |
| 1757 | int ret; |
| 1758 | /* |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1759 | * Shutdown the active I_T nexus if 'NULL' is passed. |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1760 | */ |
| 1761 | if (!strncmp(page, "NULL", 4)) { |
| 1762 | ret = scsiback_drop_nexus(tpg); |
| 1763 | return (!ret) ? count : ret; |
| 1764 | } |
| 1765 | /* |
| 1766 | * Otherwise make sure the passed virtual Initiator port WWN matches |
| 1767 | * the fabric protocol_id set in scsiback_make_tport(), and call |
| 1768 | * scsiback_make_nexus(). |
| 1769 | */ |
| 1770 | if (strlen(page) >= VSCSI_NAMELEN) { |
| 1771 | pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n", |
| 1772 | page, VSCSI_NAMELEN); |
| 1773 | return -EINVAL; |
| 1774 | } |
| 1775 | snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page); |
| 1776 | |
| 1777 | ptr = strstr(i_port, "naa."); |
| 1778 | if (ptr) { |
| 1779 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { |
| 1780 | pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n", |
| 1781 | i_port, scsiback_dump_proto_id(tport_wwn)); |
| 1782 | return -EINVAL; |
| 1783 | } |
| 1784 | port_ptr = &i_port[0]; |
| 1785 | goto check_newline; |
| 1786 | } |
| 1787 | ptr = strstr(i_port, "fc."); |
| 1788 | if (ptr) { |
| 1789 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { |
| 1790 | pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n", |
| 1791 | i_port, scsiback_dump_proto_id(tport_wwn)); |
| 1792 | return -EINVAL; |
| 1793 | } |
| 1794 | port_ptr = &i_port[3]; /* Skip over "fc." */ |
| 1795 | goto check_newline; |
| 1796 | } |
| 1797 | ptr = strstr(i_port, "iqn."); |
| 1798 | if (ptr) { |
| 1799 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { |
| 1800 | pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n", |
| 1801 | i_port, scsiback_dump_proto_id(tport_wwn)); |
| 1802 | return -EINVAL; |
| 1803 | } |
| 1804 | port_ptr = &i_port[0]; |
| 1805 | goto check_newline; |
| 1806 | } |
| 1807 | pr_err("Unable to locate prefix for emulated Initiator Port: %s\n", |
| 1808 | i_port); |
| 1809 | return -EINVAL; |
| 1810 | /* |
| 1811 | * Clear any trailing newline for the NAA WWN |
| 1812 | */ |
| 1813 | check_newline: |
| 1814 | if (i_port[strlen(i_port) - 1] == '\n') |
| 1815 | i_port[strlen(i_port) - 1] = '\0'; |
| 1816 | |
| 1817 | ret = scsiback_make_nexus(tpg, port_ptr); |
| 1818 | if (ret < 0) |
| 1819 | return ret; |
| 1820 | |
| 1821 | return count; |
| 1822 | } |
| 1823 | |
| 1824 | TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR); |
| 1825 | |
| 1826 | static struct configfs_attribute *scsiback_tpg_attrs[] = { |
| 1827 | &scsiback_tpg_nexus.attr, |
| 1828 | NULL, |
| 1829 | }; |
| 1830 | |
| 1831 | static ssize_t |
| 1832 | scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf, |
| 1833 | char *page) |
| 1834 | { |
| 1835 | return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on " |
| 1836 | UTS_RELEASE"\n", |
| 1837 | VSCSI_VERSION, utsname()->sysname, utsname()->machine); |
| 1838 | } |
| 1839 | |
| 1840 | TF_WWN_ATTR_RO(scsiback, version); |
| 1841 | |
| 1842 | static struct configfs_attribute *scsiback_wwn_attrs[] = { |
| 1843 | &scsiback_wwn_version.attr, |
| 1844 | NULL, |
| 1845 | }; |
| 1846 | |
| 1847 | static char *scsiback_get_fabric_name(void) |
| 1848 | { |
| 1849 | return "xen-pvscsi"; |
| 1850 | } |
| 1851 | |
| 1852 | static int scsiback_port_link(struct se_portal_group *se_tpg, |
| 1853 | struct se_lun *lun) |
| 1854 | { |
| 1855 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1856 | struct scsiback_tpg, se_tpg); |
| 1857 | |
| 1858 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1859 | tpg->tv_tpg_port_count++; |
| 1860 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1861 | |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
| 1865 | static void scsiback_port_unlink(struct se_portal_group *se_tpg, |
| 1866 | struct se_lun *lun) |
| 1867 | { |
| 1868 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1869 | struct scsiback_tpg, se_tpg); |
| 1870 | |
| 1871 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1872 | tpg->tv_tpg_port_count--; |
| 1873 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1874 | } |
| 1875 | |
| 1876 | static struct se_portal_group * |
| 1877 | scsiback_make_tpg(struct se_wwn *wwn, |
| 1878 | struct config_group *group, |
| 1879 | const char *name) |
| 1880 | { |
| 1881 | struct scsiback_tport *tport = container_of(wwn, |
| 1882 | struct scsiback_tport, tport_wwn); |
| 1883 | |
| 1884 | struct scsiback_tpg *tpg; |
Dan Carpenter | 495daef | 2014-09-08 14:17:35 +0300 | [diff] [blame] | 1885 | u16 tpgt; |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1886 | int ret; |
| 1887 | |
| 1888 | if (strstr(name, "tpgt_") != name) |
| 1889 | return ERR_PTR(-EINVAL); |
Dan Carpenter | 495daef | 2014-09-08 14:17:35 +0300 | [diff] [blame] | 1890 | ret = kstrtou16(name + 5, 10, &tpgt); |
| 1891 | if (ret) |
| 1892 | return ERR_PTR(ret); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1893 | |
| 1894 | tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL); |
| 1895 | if (!tpg) |
| 1896 | return ERR_PTR(-ENOMEM); |
| 1897 | |
| 1898 | mutex_init(&tpg->tv_tpg_mutex); |
| 1899 | INIT_LIST_HEAD(&tpg->tv_tpg_list); |
| 1900 | INIT_LIST_HEAD(&tpg->info_list); |
| 1901 | tpg->tport = tport; |
| 1902 | tpg->tport_tpgt = tpgt; |
| 1903 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1904 | ret = core_tpg_register(&scsiback_ops, wwn, |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1905 | &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL); |
| 1906 | if (ret < 0) { |
| 1907 | kfree(tpg); |
| 1908 | return NULL; |
| 1909 | } |
| 1910 | mutex_lock(&scsiback_mutex); |
| 1911 | list_add_tail(&tpg->tv_tpg_list, &scsiback_list); |
| 1912 | mutex_unlock(&scsiback_mutex); |
| 1913 | |
| 1914 | return &tpg->se_tpg; |
| 1915 | } |
| 1916 | |
| 1917 | static void scsiback_drop_tpg(struct se_portal_group *se_tpg) |
| 1918 | { |
| 1919 | struct scsiback_tpg *tpg = container_of(se_tpg, |
| 1920 | struct scsiback_tpg, se_tpg); |
| 1921 | |
| 1922 | mutex_lock(&scsiback_mutex); |
| 1923 | list_del(&tpg->tv_tpg_list); |
| 1924 | mutex_unlock(&scsiback_mutex); |
| 1925 | /* |
| 1926 | * Release the virtual I_T Nexus for this xen-pvscsi TPG |
| 1927 | */ |
| 1928 | scsiback_drop_nexus(tpg); |
| 1929 | /* |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 1930 | * Deregister the se_tpg from TCM. |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1931 | */ |
| 1932 | core_tpg_deregister(se_tpg); |
| 1933 | kfree(tpg); |
| 1934 | } |
| 1935 | |
| 1936 | static int scsiback_check_true(struct se_portal_group *se_tpg) |
| 1937 | { |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
| 1941 | static int scsiback_check_false(struct se_portal_group *se_tpg) |
| 1942 | { |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1946 | static const struct target_core_fabric_ops scsiback_ops = { |
| 1947 | .module = THIS_MODULE, |
| 1948 | .name = "xen-pvscsi", |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1949 | .get_fabric_name = scsiback_get_fabric_name, |
| 1950 | .get_fabric_proto_ident = scsiback_get_fabric_proto_ident, |
| 1951 | .tpg_get_wwn = scsiback_get_fabric_wwn, |
| 1952 | .tpg_get_tag = scsiback_get_tag, |
| 1953 | .tpg_get_default_depth = scsiback_get_default_depth, |
| 1954 | .tpg_get_pr_transport_id = scsiback_get_pr_transport_id, |
| 1955 | .tpg_get_pr_transport_id_len = scsiback_get_pr_transport_id_len, |
| 1956 | .tpg_parse_pr_out_transport_id = scsiback_parse_pr_out_transport_id, |
| 1957 | .tpg_check_demo_mode = scsiback_check_true, |
| 1958 | .tpg_check_demo_mode_cache = scsiback_check_true, |
| 1959 | .tpg_check_demo_mode_write_protect = scsiback_check_false, |
| 1960 | .tpg_check_prod_mode_write_protect = scsiback_check_false, |
| 1961 | .tpg_alloc_fabric_acl = scsiback_alloc_fabric_acl, |
| 1962 | .tpg_release_fabric_acl = scsiback_release_fabric_acl, |
| 1963 | .tpg_get_inst_index = scsiback_tpg_get_inst_index, |
| 1964 | .check_stop_free = scsiback_check_stop_free, |
| 1965 | .release_cmd = scsiback_release_cmd, |
| 1966 | .put_session = NULL, |
| 1967 | .shutdown_session = scsiback_shutdown_session, |
| 1968 | .close_session = scsiback_close_session, |
| 1969 | .sess_get_index = scsiback_sess_get_index, |
| 1970 | .sess_get_initiator_sid = NULL, |
| 1971 | .write_pending = scsiback_write_pending, |
| 1972 | .write_pending_status = scsiback_write_pending_status, |
| 1973 | .set_default_node_attributes = scsiback_set_default_node_attrs, |
| 1974 | .get_task_tag = scsiback_get_task_tag, |
| 1975 | .get_cmd_state = scsiback_get_cmd_state, |
| 1976 | .queue_data_in = scsiback_queue_data_in, |
| 1977 | .queue_status = scsiback_queue_status, |
| 1978 | .queue_tm_rsp = scsiback_queue_tm_rsp, |
| 1979 | .aborted_task = scsiback_aborted_task, |
| 1980 | /* |
| 1981 | * Setup callers for generic logic in target_core_fabric_configfs.c |
| 1982 | */ |
| 1983 | .fabric_make_wwn = scsiback_make_tport, |
| 1984 | .fabric_drop_wwn = scsiback_drop_tport, |
| 1985 | .fabric_make_tpg = scsiback_make_tpg, |
| 1986 | .fabric_drop_tpg = scsiback_drop_tpg, |
| 1987 | .fabric_post_link = scsiback_port_link, |
| 1988 | .fabric_pre_unlink = scsiback_port_unlink, |
| 1989 | .fabric_make_np = NULL, |
| 1990 | .fabric_drop_np = NULL, |
| 1991 | #if 0 |
| 1992 | .fabric_make_nodeacl = scsiback_make_nodeacl, |
| 1993 | .fabric_drop_nodeacl = scsiback_drop_nodeacl, |
| 1994 | #endif |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1995 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1996 | .tfc_wwn_attrs = scsiback_wwn_attrs, |
| 1997 | .tfc_tpg_base_attrs = scsiback_tpg_attrs, |
| 1998 | .tfc_tpg_param_attrs = scsiback_param_attrs, |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 1999 | }; |
| 2000 | |
| 2001 | static const struct xenbus_device_id scsiback_ids[] = { |
| 2002 | { "vscsi" }, |
| 2003 | { "" } |
| 2004 | }; |
| 2005 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2006 | static struct xenbus_driver scsiback_driver = { |
| 2007 | .ids = scsiback_ids, |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2008 | .probe = scsiback_probe, |
| 2009 | .remove = scsiback_remove, |
| 2010 | .otherend_changed = scsiback_frontend_changed |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2011 | }; |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2012 | |
| 2013 | static void scsiback_init_pend(void *p) |
| 2014 | { |
| 2015 | struct vscsibk_pend *pend = p; |
| 2016 | int i; |
| 2017 | |
| 2018 | memset(pend, 0, sizeof(*pend)); |
| 2019 | for (i = 0; i < VSCSI_MAX_GRANTS; i++) |
| 2020 | pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE; |
| 2021 | } |
| 2022 | |
| 2023 | static int __init scsiback_init(void) |
| 2024 | { |
| 2025 | int ret; |
| 2026 | |
| 2027 | if (!xen_domain()) |
| 2028 | return -ENODEV; |
| 2029 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 2030 | pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n", |
| 2031 | VSCSI_VERSION, utsname()->sysname, utsname()->machine); |
| 2032 | |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2033 | scsiback_cachep = kmem_cache_create("vscsiif_cache", |
| 2034 | sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend); |
| 2035 | if (!scsiback_cachep) |
| 2036 | return -ENOMEM; |
| 2037 | |
| 2038 | ret = xenbus_register_backend(&scsiback_driver); |
| 2039 | if (ret) |
| 2040 | goto out_cache_destroy; |
| 2041 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 2042 | ret = target_register_template(&scsiback_ops); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2043 | if (ret) |
| 2044 | goto out_unregister_xenbus; |
| 2045 | |
| 2046 | return 0; |
| 2047 | |
| 2048 | out_unregister_xenbus: |
| 2049 | xenbus_unregister_driver(&scsiback_driver); |
| 2050 | out_cache_destroy: |
| 2051 | kmem_cache_destroy(scsiback_cachep); |
Tao Chen | 7857487 | 2015-03-10 20:49:18 +0000 | [diff] [blame] | 2052 | pr_err("%s: error %d\n", __func__, ret); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2053 | return ret; |
| 2054 | } |
| 2055 | |
| 2056 | static void __exit scsiback_exit(void) |
| 2057 | { |
| 2058 | struct page *page; |
| 2059 | |
| 2060 | while (free_pages_num) { |
| 2061 | if (get_free_page(&page)) |
| 2062 | BUG(); |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 2063 | gnttab_free_pages(1, &page); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2064 | } |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 2065 | target_unregister_template(&scsiback_ops); |
Juergen Gross | d9d660f | 2014-08-28 06:44:12 +0200 | [diff] [blame] | 2066 | xenbus_unregister_driver(&scsiback_driver); |
| 2067 | kmem_cache_destroy(scsiback_cachep); |
| 2068 | } |
| 2069 | |
| 2070 | module_init(scsiback_init); |
| 2071 | module_exit(scsiback_exit); |
| 2072 | |
| 2073 | MODULE_DESCRIPTION("Xen SCSI backend driver"); |
| 2074 | MODULE_LICENSE("Dual BSD/GPL"); |
| 2075 | MODULE_ALIAS("xen-backend:vscsi"); |
| 2076 | MODULE_AUTHOR("Juergen Gross <jgross@suse.com>"); |