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