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 52ba929992SBart Van Assche #include <scsi/scsi_host.h> /* SG_ALL */ 53d9d660f6SJuergen Gross 54d9d660f6SJuergen Gross #include <target/target_core_base.h> 55d9d660f6SJuergen Gross #include <target/target_core_fabric.h> 56d9d660f6SJuergen Gross 57d9d660f6SJuergen Gross #include <asm/hypervisor.h> 58d9d660f6SJuergen Gross 59d9d660f6SJuergen Gross #include <xen/xen.h> 60d9d660f6SJuergen Gross #include <xen/balloon.h> 61d9d660f6SJuergen Gross #include <xen/events.h> 62d9d660f6SJuergen Gross #include <xen/xenbus.h> 63d9d660f6SJuergen Gross #include <xen/grant_table.h> 64d9d660f6SJuergen Gross #include <xen/page.h> 65d9d660f6SJuergen Gross 66d9d660f6SJuergen Gross #include <xen/interface/grant_table.h> 67d9d660f6SJuergen Gross #include <xen/interface/io/vscsiif.h> 68d9d660f6SJuergen Gross 69d9d660f6SJuergen Gross #define VSCSI_VERSION "v0.1" 70d9d660f6SJuergen Gross #define VSCSI_NAMELEN 32 71d9d660f6SJuergen Gross 72d9d660f6SJuergen Gross struct ids_tuple { 73d9d660f6SJuergen Gross unsigned int hst; /* host */ 74d9d660f6SJuergen Gross unsigned int chn; /* channel */ 75d9d660f6SJuergen Gross unsigned int tgt; /* target */ 76d9d660f6SJuergen Gross unsigned int lun; /* LUN */ 77d9d660f6SJuergen Gross }; 78d9d660f6SJuergen Gross 79d9d660f6SJuergen Gross struct v2p_entry { 80d9d660f6SJuergen Gross struct ids_tuple v; /* translate from */ 81d9d660f6SJuergen Gross struct scsiback_tpg *tpg; /* translate to */ 82d9d660f6SJuergen Gross unsigned int lun; 83d9d660f6SJuergen Gross struct kref kref; 84d9d660f6SJuergen Gross struct list_head l; 85d9d660f6SJuergen Gross }; 86d9d660f6SJuergen Gross 87d9d660f6SJuergen Gross struct vscsibk_info { 88d9d660f6SJuergen Gross struct xenbus_device *dev; 89d9d660f6SJuergen Gross 90d9d660f6SJuergen Gross domid_t domid; 91d9d660f6SJuergen Gross unsigned int irq; 92d9d660f6SJuergen Gross 93d9d660f6SJuergen Gross struct vscsiif_back_ring ring; 94d9d660f6SJuergen Gross 95d9d660f6SJuergen Gross spinlock_t ring_lock; 96d9d660f6SJuergen Gross atomic_t nr_unreplied_reqs; 97d9d660f6SJuergen Gross 98d9d660f6SJuergen Gross spinlock_t v2p_lock; 99d9d660f6SJuergen Gross struct list_head v2p_entry_lists; 100d9d660f6SJuergen Gross 101d9d660f6SJuergen Gross wait_queue_head_t waiting_to_free; 102ca33479cSJuergen Gross 103ca33479cSJuergen Gross struct gnttab_page_cache free_pages; 104d9d660f6SJuergen Gross }; 105d9d660f6SJuergen Gross 106d9d660f6SJuergen Gross /* theoretical maximum of grants for one request */ 107d9d660f6SJuergen Gross #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE) 108d9d660f6SJuergen Gross 109d9d660f6SJuergen Gross /* 110d9d660f6SJuergen Gross * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one 111d9d660f6SJuergen Gross * call to map/unmap grants. Don't choose it too large, as there are arrays 112d9d660f6SJuergen Gross * with VSCSI_GRANT_BATCH elements allocated on the stack. 113d9d660f6SJuergen Gross */ 114d9d660f6SJuergen Gross #define VSCSI_GRANT_BATCH 16 115d9d660f6SJuergen Gross 116d9d660f6SJuergen Gross struct vscsibk_pend { 117d9d660f6SJuergen Gross uint16_t rqid; 118d9d660f6SJuergen Gross 119d9d660f6SJuergen Gross uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE]; 120d9d660f6SJuergen Gross uint8_t cmd_len; 121d9d660f6SJuergen Gross 122d9d660f6SJuergen Gross uint8_t sc_data_direction; 123d9d660f6SJuergen Gross uint16_t n_sg; /* real length of SG list */ 124d9d660f6SJuergen Gross uint16_t n_grants; /* SG pages and potentially SG list */ 125d9d660f6SJuergen Gross uint32_t data_len; 126d9d660f6SJuergen Gross uint32_t result; 127d9d660f6SJuergen Gross 128d9d660f6SJuergen Gross struct vscsibk_info *info; 129d9d660f6SJuergen Gross struct v2p_entry *v2p; 130d9d660f6SJuergen Gross struct scatterlist *sgl; 131d9d660f6SJuergen Gross 132d9d660f6SJuergen Gross uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE]; 133d9d660f6SJuergen Gross 134d9d660f6SJuergen Gross grant_handle_t grant_handles[VSCSI_MAX_GRANTS]; 135d9d660f6SJuergen Gross struct page *pages[VSCSI_MAX_GRANTS]; 136d9d660f6SJuergen Gross 137d9d660f6SJuergen Gross struct se_cmd se_cmd; 138d9d660f6SJuergen Gross 139e3eac124SBart Van Assche struct completion tmr_done; 140d9d660f6SJuergen Gross }; 141d9d660f6SJuergen Gross 1422dbcdf33SNicholas Bellinger #define VSCSI_DEFAULT_SESSION_TAGS 128 1432dbcdf33SNicholas Bellinger 144d9d660f6SJuergen Gross struct scsiback_nexus { 145d9d660f6SJuergen Gross /* Pointer to TCM session for I_T Nexus */ 146d9d660f6SJuergen Gross struct se_session *tvn_se_sess; 147d9d660f6SJuergen Gross }; 148d9d660f6SJuergen Gross 149d9d660f6SJuergen Gross struct scsiback_tport { 150d9d660f6SJuergen Gross /* SCSI protocol the tport is providing */ 151d9d660f6SJuergen Gross u8 tport_proto_id; 152d9d660f6SJuergen Gross /* Binary World Wide unique Port Name for pvscsi Target port */ 153d9d660f6SJuergen Gross u64 tport_wwpn; 154d9d660f6SJuergen Gross /* ASCII formatted WWPN for pvscsi Target port */ 155d9d660f6SJuergen Gross char tport_name[VSCSI_NAMELEN]; 156d9d660f6SJuergen Gross /* Returned by scsiback_make_tport() */ 157d9d660f6SJuergen Gross struct se_wwn tport_wwn; 158d9d660f6SJuergen Gross }; 159d9d660f6SJuergen Gross 160d9d660f6SJuergen Gross struct scsiback_tpg { 161d9d660f6SJuergen Gross /* scsiback port target portal group tag for TCM */ 162d9d660f6SJuergen Gross u16 tport_tpgt; 163d9d660f6SJuergen Gross /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */ 164d9d660f6SJuergen Gross int tv_tpg_port_count; 165d9d660f6SJuergen Gross /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */ 166d9d660f6SJuergen Gross int tv_tpg_fe_count; 167d9d660f6SJuergen Gross /* list for scsiback_list */ 168d9d660f6SJuergen Gross struct list_head tv_tpg_list; 169d9d660f6SJuergen Gross /* Used to protect access for tpg_nexus */ 170d9d660f6SJuergen Gross struct mutex tv_tpg_mutex; 171d9d660f6SJuergen Gross /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */ 172d9d660f6SJuergen Gross struct scsiback_nexus *tpg_nexus; 173d9d660f6SJuergen Gross /* Pointer back to scsiback_tport */ 174d9d660f6SJuergen Gross struct scsiback_tport *tport; 175d9d660f6SJuergen Gross /* Returned by scsiback_make_tpg() */ 176d9d660f6SJuergen Gross struct se_portal_group se_tpg; 177d9d660f6SJuergen Gross /* alias used in xenstore */ 178d9d660f6SJuergen Gross char param_alias[VSCSI_NAMELEN]; 179d9d660f6SJuergen Gross /* list of info structures related to this target portal group */ 180d9d660f6SJuergen Gross struct list_head info_list; 181d9d660f6SJuergen Gross }; 182d9d660f6SJuergen Gross 183d9d660f6SJuergen Gross #define SCSIBACK_INVALID_HANDLE (~0) 184d9d660f6SJuergen Gross 185d9d660f6SJuergen Gross static bool log_print_stat; 186d9d660f6SJuergen Gross module_param(log_print_stat, bool, 0644); 187d9d660f6SJuergen Gross 188d9d660f6SJuergen Gross static int scsiback_max_buffer_pages = 1024; 189d9d660f6SJuergen Gross module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644); 190d9d660f6SJuergen Gross MODULE_PARM_DESC(max_buffer_pages, 191d9d660f6SJuergen Gross "Maximum number of free pages to keep in backend buffer"); 192d9d660f6SJuergen Gross 193d9d660f6SJuergen Gross /* Global spinlock to protect scsiback TPG list */ 194d9d660f6SJuergen Gross static DEFINE_MUTEX(scsiback_mutex); 195d9d660f6SJuergen Gross static LIST_HEAD(scsiback_list); 196d9d660f6SJuergen Gross 197d9d660f6SJuergen Gross static void scsiback_get(struct vscsibk_info *info) 198d9d660f6SJuergen Gross { 199d9d660f6SJuergen Gross atomic_inc(&info->nr_unreplied_reqs); 200d9d660f6SJuergen Gross } 201d9d660f6SJuergen Gross 202d9d660f6SJuergen Gross static void scsiback_put(struct vscsibk_info *info) 203d9d660f6SJuergen Gross { 204d9d660f6SJuergen Gross if (atomic_dec_and_test(&info->nr_unreplied_reqs)) 205d9d660f6SJuergen Gross wake_up(&info->waiting_to_free); 206d9d660f6SJuergen Gross } 207d9d660f6SJuergen Gross 208d9d660f6SJuergen Gross static unsigned long vaddr_page(struct page *page) 209d9d660f6SJuergen Gross { 210d9d660f6SJuergen Gross unsigned long pfn = page_to_pfn(page); 211d9d660f6SJuergen Gross 212d9d660f6SJuergen Gross return (unsigned long)pfn_to_kaddr(pfn); 213d9d660f6SJuergen Gross } 214d9d660f6SJuergen Gross 215d9d660f6SJuergen Gross static unsigned long vaddr(struct vscsibk_pend *req, int seg) 216d9d660f6SJuergen Gross { 217d9d660f6SJuergen Gross return vaddr_page(req->pages[seg]); 218d9d660f6SJuergen Gross } 219d9d660f6SJuergen Gross 220d9d660f6SJuergen Gross static void scsiback_print_status(char *sense_buffer, int errors, 221d9d660f6SJuergen Gross struct vscsibk_pend *pending_req) 222d9d660f6SJuergen Gross { 223d9d660f6SJuergen Gross struct scsiback_tpg *tpg = pending_req->v2p->tpg; 224d9d660f6SJuergen Gross 22578574878STao Chen pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n", 226d9d660f6SJuergen Gross tpg->tport->tport_name, pending_req->v2p->lun, 227d9d660f6SJuergen Gross pending_req->cmnd[0], status_byte(errors), msg_byte(errors), 228d9d660f6SJuergen Gross host_byte(errors), driver_byte(errors)); 229d9d660f6SJuergen Gross } 230d9d660f6SJuergen Gross 231d9d660f6SJuergen Gross static void scsiback_fast_flush_area(struct vscsibk_pend *req) 232d9d660f6SJuergen Gross { 233d9d660f6SJuergen Gross struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH]; 234d9d660f6SJuergen Gross struct page *pages[VSCSI_GRANT_BATCH]; 235d9d660f6SJuergen Gross unsigned int i, invcount = 0; 236d9d660f6SJuergen Gross grant_handle_t handle; 237d9d660f6SJuergen Gross int err; 238d9d660f6SJuergen Gross 239d9d660f6SJuergen Gross kfree(req->sgl); 240d9d660f6SJuergen Gross req->sgl = NULL; 241d9d660f6SJuergen Gross req->n_sg = 0; 242d9d660f6SJuergen Gross 243d9d660f6SJuergen Gross if (!req->n_grants) 244d9d660f6SJuergen Gross return; 245d9d660f6SJuergen Gross 246d9d660f6SJuergen Gross for (i = 0; i < req->n_grants; i++) { 247d9d660f6SJuergen Gross handle = req->grant_handles[i]; 248d9d660f6SJuergen Gross if (handle == SCSIBACK_INVALID_HANDLE) 249d9d660f6SJuergen Gross continue; 250d9d660f6SJuergen Gross gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i), 251d9d660f6SJuergen Gross GNTMAP_host_map, handle); 252d9d660f6SJuergen Gross req->grant_handles[i] = SCSIBACK_INVALID_HANDLE; 253d9d660f6SJuergen Gross pages[invcount] = req->pages[i]; 254d9d660f6SJuergen Gross put_page(pages[invcount]); 255d9d660f6SJuergen Gross invcount++; 256d9d660f6SJuergen Gross if (invcount < VSCSI_GRANT_BATCH) 257d9d660f6SJuergen Gross continue; 258d9d660f6SJuergen Gross err = gnttab_unmap_refs(unmap, NULL, pages, invcount); 259d9d660f6SJuergen Gross BUG_ON(err); 260d9d660f6SJuergen Gross invcount = 0; 261d9d660f6SJuergen Gross } 262d9d660f6SJuergen Gross 263d9d660f6SJuergen Gross if (invcount) { 264d9d660f6SJuergen Gross err = gnttab_unmap_refs(unmap, NULL, pages, invcount); 265d9d660f6SJuergen Gross BUG_ON(err); 266d9d660f6SJuergen Gross } 267d9d660f6SJuergen Gross 268ca33479cSJuergen Gross gnttab_page_cache_put(&req->info->free_pages, req->pages, 269ca33479cSJuergen Gross req->n_grants); 270d9d660f6SJuergen Gross req->n_grants = 0; 271d9d660f6SJuergen Gross } 272d9d660f6SJuergen Gross 273d9d660f6SJuergen Gross static void scsiback_free_translation_entry(struct kref *kref) 274d9d660f6SJuergen Gross { 275d9d660f6SJuergen Gross struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref); 276d9d660f6SJuergen Gross struct scsiback_tpg *tpg = entry->tpg; 277d9d660f6SJuergen Gross 278d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 279d9d660f6SJuergen Gross tpg->tv_tpg_fe_count--; 280d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 281d9d660f6SJuergen Gross 282d9d660f6SJuergen Gross kfree(entry); 283d9d660f6SJuergen Gross } 284d9d660f6SJuergen Gross 2852dbcdf33SNicholas Bellinger static void scsiback_send_response(struct vscsibk_info *info, 2862dbcdf33SNicholas Bellinger char *sense_buffer, int32_t result, uint32_t resid, 2872dbcdf33SNicholas Bellinger uint16_t rqid) 288d9d660f6SJuergen Gross { 289d9d660f6SJuergen Gross struct vscsiif_response *ring_res; 290d9d660f6SJuergen Gross int notify; 291d9d660f6SJuergen Gross struct scsi_sense_hdr sshdr; 292d9d660f6SJuergen Gross unsigned long flags; 293d9d660f6SJuergen Gross unsigned len; 294d9d660f6SJuergen Gross 295d9d660f6SJuergen Gross spin_lock_irqsave(&info->ring_lock, flags); 296d9d660f6SJuergen Gross 297d9d660f6SJuergen Gross ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt); 298d9d660f6SJuergen Gross info->ring.rsp_prod_pvt++; 299d9d660f6SJuergen Gross 300d9d660f6SJuergen Gross ring_res->rslt = result; 3012dbcdf33SNicholas Bellinger ring_res->rqid = rqid; 302d9d660f6SJuergen Gross 303d9d660f6SJuergen Gross if (sense_buffer != NULL && 304d9d660f6SJuergen Gross scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE, 305d9d660f6SJuergen Gross &sshdr)) { 306d9d660f6SJuergen Gross len = min_t(unsigned, 8 + sense_buffer[7], 307d9d660f6SJuergen Gross VSCSIIF_SENSE_BUFFERSIZE); 308d9d660f6SJuergen Gross memcpy(ring_res->sense_buffer, sense_buffer, len); 309d9d660f6SJuergen Gross ring_res->sense_len = len; 310d9d660f6SJuergen Gross } else { 311d9d660f6SJuergen Gross ring_res->sense_len = 0; 312d9d660f6SJuergen Gross } 313d9d660f6SJuergen Gross 314d9d660f6SJuergen Gross ring_res->residual_len = resid; 315d9d660f6SJuergen Gross 316d9d660f6SJuergen Gross RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify); 317d9d660f6SJuergen Gross spin_unlock_irqrestore(&info->ring_lock, flags); 318d9d660f6SJuergen Gross 319d9d660f6SJuergen Gross if (notify) 320d9d660f6SJuergen Gross notify_remote_via_irq(info->irq); 3212dbcdf33SNicholas Bellinger } 3222dbcdf33SNicholas Bellinger 3232dbcdf33SNicholas Bellinger static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result, 3242dbcdf33SNicholas Bellinger uint32_t resid, struct vscsibk_pend *pending_req) 3252dbcdf33SNicholas Bellinger { 3262dbcdf33SNicholas Bellinger scsiback_send_response(pending_req->info, sense_buffer, result, 3272dbcdf33SNicholas Bellinger resid, pending_req->rqid); 328d9d660f6SJuergen Gross 329d9d660f6SJuergen Gross if (pending_req->v2p) 330d9d660f6SJuergen Gross kref_put(&pending_req->v2p->kref, 331d9d660f6SJuergen Gross scsiback_free_translation_entry); 332d9d660f6SJuergen Gross } 333d9d660f6SJuergen Gross 334d9d660f6SJuergen Gross static void scsiback_cmd_done(struct vscsibk_pend *pending_req) 335d9d660f6SJuergen Gross { 336d9d660f6SJuergen Gross struct vscsibk_info *info = pending_req->info; 337d9d660f6SJuergen Gross unsigned char *sense_buffer; 338d9d660f6SJuergen Gross unsigned int resid; 339d9d660f6SJuergen Gross int errors; 340d9d660f6SJuergen Gross 341d9d660f6SJuergen Gross sense_buffer = pending_req->sense_buffer; 342d9d660f6SJuergen Gross resid = pending_req->se_cmd.residual_count; 343d9d660f6SJuergen Gross errors = pending_req->result; 344d9d660f6SJuergen Gross 345d9d660f6SJuergen Gross if (errors && log_print_stat) 346d9d660f6SJuergen Gross scsiback_print_status(sense_buffer, errors, pending_req); 347d9d660f6SJuergen Gross 348d9d660f6SJuergen Gross scsiback_fast_flush_area(pending_req); 349d9d660f6SJuergen Gross scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req); 350d9d660f6SJuergen Gross scsiback_put(info); 351fa22e7b7SNicholas Bellinger /* 352fa22e7b7SNicholas Bellinger * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls() 353fa22e7b7SNicholas Bellinger * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd() 354fa22e7b7SNicholas Bellinger * final se_cmd->cmd_kref put. 355fa22e7b7SNicholas Bellinger */ 356fa22e7b7SNicholas Bellinger target_put_sess_cmd(&pending_req->se_cmd); 357d9d660f6SJuergen Gross } 358d9d660f6SJuergen Gross 359d9d660f6SJuergen Gross static void scsiback_cmd_exec(struct vscsibk_pend *pending_req) 360d9d660f6SJuergen Gross { 361d9d660f6SJuergen Gross struct se_cmd *se_cmd = &pending_req->se_cmd; 362d9d660f6SJuergen Gross struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess; 363d9d660f6SJuergen Gross int rc; 364d9d660f6SJuergen Gross 365d9d660f6SJuergen Gross scsiback_get(pending_req->info); 366649ee054SBart Van Assche se_cmd->tag = pending_req->rqid; 367d9d660f6SJuergen Gross rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd, 368d9d660f6SJuergen Gross pending_req->sense_buffer, pending_req->v2p->lun, 369d9d660f6SJuergen Gross pending_req->data_len, 0, 370fa22e7b7SNicholas Bellinger pending_req->sc_data_direction, TARGET_SCF_ACK_KREF, 371d9d660f6SJuergen Gross pending_req->sgl, pending_req->n_sg, 372d9d660f6SJuergen Gross NULL, 0, NULL, 0); 373d9d660f6SJuergen Gross if (rc < 0) { 374d9d660f6SJuergen Gross transport_send_check_condition_and_sense(se_cmd, 375d9d660f6SJuergen Gross TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); 376d9d660f6SJuergen Gross transport_generic_free_cmd(se_cmd, 0); 377d9d660f6SJuergen Gross } 378d9d660f6SJuergen Gross } 379d9d660f6SJuergen Gross 380d9d660f6SJuergen Gross static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map, 381d9d660f6SJuergen Gross struct page **pg, grant_handle_t *grant, int cnt) 382d9d660f6SJuergen Gross { 383d9d660f6SJuergen Gross int err, i; 384d9d660f6SJuergen Gross 385d9d660f6SJuergen Gross if (!cnt) 386d9d660f6SJuergen Gross return 0; 387d9d660f6SJuergen Gross 388d9d660f6SJuergen Gross err = gnttab_map_refs(map, NULL, pg, cnt); 389d9d660f6SJuergen Gross BUG_ON(err); 390d9d660f6SJuergen Gross for (i = 0; i < cnt; i++) { 391d9d660f6SJuergen Gross if (unlikely(map[i].status != GNTST_okay)) { 39278574878STao Chen pr_err("invalid buffer -- could not remap it\n"); 393d9d660f6SJuergen Gross map[i].handle = SCSIBACK_INVALID_HANDLE; 394d9d660f6SJuergen Gross err = -ENOMEM; 395d9d660f6SJuergen Gross } else { 396d9d660f6SJuergen Gross get_page(pg[i]); 397d9d660f6SJuergen Gross } 398d9d660f6SJuergen Gross grant[i] = map[i].handle; 399d9d660f6SJuergen Gross } 400d9d660f6SJuergen Gross return err; 401d9d660f6SJuergen Gross } 402d9d660f6SJuergen Gross 403d9d660f6SJuergen Gross static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req, 404d9d660f6SJuergen Gross struct scsiif_request_segment *seg, struct page **pg, 405d9d660f6SJuergen Gross grant_handle_t *grant, int cnt, u32 flags) 406d9d660f6SJuergen Gross { 407d9d660f6SJuergen Gross int mapcount = 0, i, err = 0; 408d9d660f6SJuergen Gross struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH]; 409d9d660f6SJuergen Gross struct vscsibk_info *info = pending_req->info; 410d9d660f6SJuergen Gross 411d9d660f6SJuergen Gross for (i = 0; i < cnt; i++) { 412ca33479cSJuergen Gross if (gnttab_page_cache_get(&info->free_pages, pg + mapcount)) { 413ca33479cSJuergen Gross gnttab_page_cache_put(&info->free_pages, pg, mapcount); 41478574878STao Chen pr_err("no grant page\n"); 415d9d660f6SJuergen Gross return -ENOMEM; 416d9d660f6SJuergen Gross } 417d9d660f6SJuergen Gross gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]), 418d9d660f6SJuergen Gross flags, seg[i].gref, info->domid); 419d9d660f6SJuergen Gross mapcount++; 420d9d660f6SJuergen Gross if (mapcount < VSCSI_GRANT_BATCH) 421d9d660f6SJuergen Gross continue; 422d9d660f6SJuergen Gross err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount); 423d9d660f6SJuergen Gross pg += mapcount; 424d9d660f6SJuergen Gross grant += mapcount; 425d9d660f6SJuergen Gross pending_req->n_grants += mapcount; 426d9d660f6SJuergen Gross if (err) 427d9d660f6SJuergen Gross return err; 428d9d660f6SJuergen Gross mapcount = 0; 429d9d660f6SJuergen Gross } 430d9d660f6SJuergen Gross err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount); 431d9d660f6SJuergen Gross pending_req->n_grants += mapcount; 432d9d660f6SJuergen Gross return err; 433d9d660f6SJuergen Gross } 434d9d660f6SJuergen Gross 435d9d660f6SJuergen Gross static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req, 436d9d660f6SJuergen Gross struct vscsibk_pend *pending_req) 437d9d660f6SJuergen Gross { 438d9d660f6SJuergen Gross u32 flags; 439d9d660f6SJuergen Gross int i, err, n_segs, i_seg = 0; 440d9d660f6SJuergen Gross struct page **pg; 441d9d660f6SJuergen Gross struct scsiif_request_segment *seg; 442d9d660f6SJuergen Gross unsigned long end_seg = 0; 443d9d660f6SJuergen Gross unsigned int nr_segments = (unsigned int)ring_req->nr_segments; 444d9d660f6SJuergen Gross unsigned int nr_sgl = 0; 445d9d660f6SJuergen Gross struct scatterlist *sg; 446d9d660f6SJuergen Gross grant_handle_t *grant; 447d9d660f6SJuergen Gross 448d9d660f6SJuergen Gross pending_req->n_sg = 0; 449d9d660f6SJuergen Gross pending_req->n_grants = 0; 450d9d660f6SJuergen Gross pending_req->data_len = 0; 451d9d660f6SJuergen Gross 452d9d660f6SJuergen Gross nr_segments &= ~VSCSIIF_SG_GRANT; 453d9d660f6SJuergen Gross if (!nr_segments) 454d9d660f6SJuergen Gross return 0; 455d9d660f6SJuergen Gross 456d9d660f6SJuergen Gross if (nr_segments > VSCSIIF_SG_TABLESIZE) { 45778574878STao Chen pr_debug("invalid parameter nr_seg = %d\n", 458d9d660f6SJuergen Gross ring_req->nr_segments); 459d9d660f6SJuergen Gross return -EINVAL; 460d9d660f6SJuergen Gross } 461d9d660f6SJuergen Gross 462d9d660f6SJuergen Gross if (ring_req->nr_segments & VSCSIIF_SG_GRANT) { 463d9d660f6SJuergen Gross err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg, 464d9d660f6SJuergen Gross pending_req->pages, pending_req->grant_handles, 465d9d660f6SJuergen Gross nr_segments, GNTMAP_host_map | GNTMAP_readonly); 466d9d660f6SJuergen Gross if (err) 467d9d660f6SJuergen Gross return err; 468d9d660f6SJuergen Gross nr_sgl = nr_segments; 469d9d660f6SJuergen Gross nr_segments = 0; 470d9d660f6SJuergen Gross for (i = 0; i < nr_sgl; i++) { 471d9d660f6SJuergen Gross n_segs = ring_req->seg[i].length / 472d9d660f6SJuergen Gross sizeof(struct scsiif_request_segment); 473d9d660f6SJuergen Gross if ((unsigned)ring_req->seg[i].offset + 474d9d660f6SJuergen Gross (unsigned)ring_req->seg[i].length > PAGE_SIZE || 475d9d660f6SJuergen Gross n_segs * sizeof(struct scsiif_request_segment) != 476d9d660f6SJuergen Gross ring_req->seg[i].length) 477d9d660f6SJuergen Gross return -EINVAL; 478d9d660f6SJuergen Gross nr_segments += n_segs; 479d9d660f6SJuergen Gross } 480d9d660f6SJuergen Gross if (nr_segments > SG_ALL) { 48178574878STao Chen pr_debug("invalid nr_seg = %d\n", nr_segments); 482d9d660f6SJuergen Gross return -EINVAL; 483d9d660f6SJuergen Gross } 484d9d660f6SJuergen Gross } 485d9d660f6SJuergen Gross 486d9d660f6SJuergen Gross /* free of (sgl) in fast_flush_area() */ 487d9d660f6SJuergen Gross pending_req->sgl = kmalloc_array(nr_segments, 488d9d660f6SJuergen Gross sizeof(struct scatterlist), GFP_KERNEL); 489d9d660f6SJuergen Gross if (!pending_req->sgl) 490d9d660f6SJuergen Gross return -ENOMEM; 491d9d660f6SJuergen Gross 492d9d660f6SJuergen Gross sg_init_table(pending_req->sgl, nr_segments); 493d9d660f6SJuergen Gross pending_req->n_sg = nr_segments; 494d9d660f6SJuergen Gross 495d9d660f6SJuergen Gross flags = GNTMAP_host_map; 496d9d660f6SJuergen Gross if (pending_req->sc_data_direction == DMA_TO_DEVICE) 497d9d660f6SJuergen Gross flags |= GNTMAP_readonly; 498d9d660f6SJuergen Gross 499d9d660f6SJuergen Gross pg = pending_req->pages + nr_sgl; 500d9d660f6SJuergen Gross grant = pending_req->grant_handles + nr_sgl; 501d9d660f6SJuergen Gross if (!nr_sgl) { 502d9d660f6SJuergen Gross seg = ring_req->seg; 503d9d660f6SJuergen Gross err = scsiback_gnttab_data_map_list(pending_req, seg, 504d9d660f6SJuergen Gross pg, grant, nr_segments, flags); 505d9d660f6SJuergen Gross if (err) 506d9d660f6SJuergen Gross return err; 507d9d660f6SJuergen Gross } else { 508d9d660f6SJuergen Gross for (i = 0; i < nr_sgl; i++) { 509d9d660f6SJuergen Gross seg = (struct scsiif_request_segment *)( 510d9d660f6SJuergen Gross vaddr(pending_req, i) + ring_req->seg[i].offset); 511d9d660f6SJuergen Gross n_segs = ring_req->seg[i].length / 512d9d660f6SJuergen Gross sizeof(struct scsiif_request_segment); 513d9d660f6SJuergen Gross err = scsiback_gnttab_data_map_list(pending_req, seg, 514d9d660f6SJuergen Gross pg, grant, n_segs, flags); 515d9d660f6SJuergen Gross if (err) 516d9d660f6SJuergen Gross return err; 517d9d660f6SJuergen Gross pg += n_segs; 518d9d660f6SJuergen Gross grant += n_segs; 519d9d660f6SJuergen Gross } 520d9d660f6SJuergen Gross end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset; 521d9d660f6SJuergen Gross seg = (struct scsiif_request_segment *)end_seg; 522d9d660f6SJuergen Gross end_seg += ring_req->seg[0].length; 523d9d660f6SJuergen Gross pg = pending_req->pages + nr_sgl; 524d9d660f6SJuergen Gross } 525d9d660f6SJuergen Gross 526d9d660f6SJuergen Gross for_each_sg(pending_req->sgl, sg, nr_segments, i) { 527d9d660f6SJuergen Gross sg_set_page(sg, pg[i], seg->length, seg->offset); 528d9d660f6SJuergen Gross pending_req->data_len += seg->length; 529d9d660f6SJuergen Gross seg++; 530d9d660f6SJuergen Gross if (nr_sgl && (unsigned long)seg >= end_seg) { 531d9d660f6SJuergen Gross i_seg++; 532d9d660f6SJuergen Gross end_seg = vaddr(pending_req, i_seg) + 533d9d660f6SJuergen Gross ring_req->seg[i_seg].offset; 534d9d660f6SJuergen Gross seg = (struct scsiif_request_segment *)end_seg; 535d9d660f6SJuergen Gross end_seg += ring_req->seg[i_seg].length; 536d9d660f6SJuergen Gross } 537d9d660f6SJuergen Gross if (sg->offset >= PAGE_SIZE || 538d9d660f6SJuergen Gross sg->length > PAGE_SIZE || 539d9d660f6SJuergen Gross sg->offset + sg->length > PAGE_SIZE) 540d9d660f6SJuergen Gross return -EINVAL; 541d9d660f6SJuergen Gross } 542d9d660f6SJuergen Gross 543d9d660f6SJuergen Gross return 0; 544d9d660f6SJuergen Gross } 545d9d660f6SJuergen Gross 546d9d660f6SJuergen Gross static void scsiback_disconnect(struct vscsibk_info *info) 547d9d660f6SJuergen Gross { 548d9d660f6SJuergen Gross wait_event(info->waiting_to_free, 549d9d660f6SJuergen Gross atomic_read(&info->nr_unreplied_reqs) == 0); 550d9d660f6SJuergen Gross 551d9d660f6SJuergen Gross unbind_from_irqhandler(info->irq, info); 552d9d660f6SJuergen Gross info->irq = 0; 553d9d660f6SJuergen Gross xenbus_unmap_ring_vfree(info->dev, info->ring.sring); 554d9d660f6SJuergen Gross } 555d9d660f6SJuergen Gross 556d9d660f6SJuergen Gross static void scsiback_device_action(struct vscsibk_pend *pending_req, 557d9d660f6SJuergen Gross enum tcm_tmreq_table act, int tag) 558d9d660f6SJuergen Gross { 559d9d660f6SJuergen Gross struct scsiback_tpg *tpg = pending_req->v2p->tpg; 560fa22e7b7SNicholas Bellinger struct scsiback_nexus *nexus = tpg->tpg_nexus; 561d9d660f6SJuergen Gross struct se_cmd *se_cmd = &pending_req->se_cmd; 562fa22e7b7SNicholas Bellinger u64 unpacked_lun = pending_req->v2p->lun; 563fa22e7b7SNicholas Bellinger int rc, err = FAILED; 564d9d660f6SJuergen Gross 565e3eac124SBart Van Assche init_completion(&pending_req->tmr_done); 566d9d660f6SJuergen Gross 567fa22e7b7SNicholas Bellinger rc = target_submit_tmr(&pending_req->se_cmd, nexus->tvn_se_sess, 568fa22e7b7SNicholas Bellinger &pending_req->sense_buffer[0], 5699f4ab18aSBart Van Assche unpacked_lun, NULL, act, GFP_KERNEL, 570fa22e7b7SNicholas Bellinger tag, TARGET_SCF_ACK_KREF); 571fa22e7b7SNicholas Bellinger if (rc) 572fa22e7b7SNicholas Bellinger goto err; 573d9d660f6SJuergen Gross 574e3eac124SBart Van Assche wait_for_completion(&pending_req->tmr_done); 575d9d660f6SJuergen Gross 576d9d660f6SJuergen Gross err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ? 577d9d660f6SJuergen Gross SUCCESS : FAILED; 578d9d660f6SJuergen Gross 579d9d660f6SJuergen Gross scsiback_do_resp_with_sense(NULL, err, 0, pending_req); 580af90e84dSBart Van Assche transport_generic_free_cmd(&pending_req->se_cmd, 0); 5812dbcdf33SNicholas Bellinger return; 5829f4ab18aSBart Van Assche 583fa22e7b7SNicholas Bellinger err: 5842dbcdf33SNicholas Bellinger scsiback_do_resp_with_sense(NULL, err, 0, pending_req); 585d9d660f6SJuergen Gross } 586d9d660f6SJuergen Gross 587d9d660f6SJuergen Gross /* 588d9d660f6SJuergen Gross Perform virtual to physical translation 589d9d660f6SJuergen Gross */ 590d9d660f6SJuergen Gross static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info, 591d9d660f6SJuergen Gross struct ids_tuple *v) 592d9d660f6SJuergen Gross { 593d9d660f6SJuergen Gross struct v2p_entry *entry; 594d9d660f6SJuergen Gross struct list_head *head = &(info->v2p_entry_lists); 595d9d660f6SJuergen Gross unsigned long flags; 596d9d660f6SJuergen Gross 597d9d660f6SJuergen Gross spin_lock_irqsave(&info->v2p_lock, flags); 598d9d660f6SJuergen Gross list_for_each_entry(entry, head, l) { 599d9d660f6SJuergen Gross if ((entry->v.chn == v->chn) && 600d9d660f6SJuergen Gross (entry->v.tgt == v->tgt) && 601d9d660f6SJuergen Gross (entry->v.lun == v->lun)) { 602d9d660f6SJuergen Gross kref_get(&entry->kref); 603d9d660f6SJuergen Gross goto out; 604d9d660f6SJuergen Gross } 605d9d660f6SJuergen Gross } 606d9d660f6SJuergen Gross entry = NULL; 607d9d660f6SJuergen Gross 608d9d660f6SJuergen Gross out: 609d9d660f6SJuergen Gross spin_unlock_irqrestore(&info->v2p_lock, flags); 610d9d660f6SJuergen Gross return entry; 611d9d660f6SJuergen Gross } 612d9d660f6SJuergen Gross 6132dbcdf33SNicholas Bellinger static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring, 6142dbcdf33SNicholas Bellinger struct v2p_entry *v2p) 615d9d660f6SJuergen Gross { 6162dbcdf33SNicholas Bellinger struct scsiback_tpg *tpg = v2p->tpg; 6172dbcdf33SNicholas Bellinger struct scsiback_nexus *nexus = tpg->tpg_nexus; 6182dbcdf33SNicholas Bellinger struct se_session *se_sess = nexus->tvn_se_sess; 6192dbcdf33SNicholas Bellinger struct vscsibk_pend *req; 62010e9cbb6SMatthew Wilcox int tag, cpu, i; 6212dbcdf33SNicholas Bellinger 62210e9cbb6SMatthew Wilcox tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu); 6232dbcdf33SNicholas Bellinger if (tag < 0) { 6242dbcdf33SNicholas Bellinger pr_err("Unable to obtain tag for vscsiif_request\n"); 6252dbcdf33SNicholas Bellinger return ERR_PTR(-ENOMEM); 6262dbcdf33SNicholas Bellinger } 6272dbcdf33SNicholas Bellinger 6282dbcdf33SNicholas Bellinger req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag]; 6292dbcdf33SNicholas Bellinger memset(req, 0, sizeof(*req)); 6302dbcdf33SNicholas Bellinger req->se_cmd.map_tag = tag; 63110e9cbb6SMatthew Wilcox req->se_cmd.map_cpu = cpu; 6322dbcdf33SNicholas Bellinger 6332dbcdf33SNicholas Bellinger for (i = 0; i < VSCSI_MAX_GRANTS; i++) 6342dbcdf33SNicholas Bellinger req->grant_handles[i] = SCSIBACK_INVALID_HANDLE; 6352dbcdf33SNicholas Bellinger 6362dbcdf33SNicholas Bellinger return req; 6372dbcdf33SNicholas Bellinger } 6382dbcdf33SNicholas Bellinger 6392dbcdf33SNicholas Bellinger static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info, 6402dbcdf33SNicholas Bellinger struct vscsiif_back_ring *ring, 6412dbcdf33SNicholas Bellinger struct vscsiif_request *ring_req) 6422dbcdf33SNicholas Bellinger { 6432dbcdf33SNicholas Bellinger struct vscsibk_pend *pending_req; 644d9d660f6SJuergen Gross struct v2p_entry *v2p; 645d9d660f6SJuergen Gross struct ids_tuple vir; 646d9d660f6SJuergen Gross 6472dbcdf33SNicholas Bellinger /* request range check from frontend */ 6482dbcdf33SNicholas Bellinger if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) && 6492dbcdf33SNicholas Bellinger (ring_req->sc_data_direction != DMA_TO_DEVICE) && 6502dbcdf33SNicholas Bellinger (ring_req->sc_data_direction != DMA_FROM_DEVICE) && 6512dbcdf33SNicholas Bellinger (ring_req->sc_data_direction != DMA_NONE)) { 6522dbcdf33SNicholas Bellinger pr_debug("invalid parameter data_dir = %d\n", 6532dbcdf33SNicholas Bellinger ring_req->sc_data_direction); 6542dbcdf33SNicholas Bellinger return ERR_PTR(-EINVAL); 6552dbcdf33SNicholas Bellinger } 6562dbcdf33SNicholas Bellinger if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) { 6572dbcdf33SNicholas Bellinger pr_debug("invalid parameter cmd_len = %d\n", 6582dbcdf33SNicholas Bellinger ring_req->cmd_len); 6592dbcdf33SNicholas Bellinger return ERR_PTR(-EINVAL); 6602dbcdf33SNicholas Bellinger } 661d9d660f6SJuergen Gross 662d9d660f6SJuergen Gross vir.chn = ring_req->channel; 663d9d660f6SJuergen Gross vir.tgt = ring_req->id; 664d9d660f6SJuergen Gross vir.lun = ring_req->lun; 665d9d660f6SJuergen Gross 666d9d660f6SJuergen Gross v2p = scsiback_do_translation(info, &vir); 667d9d660f6SJuergen Gross if (!v2p) { 66878574878STao Chen pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n", 66978574878STao Chen vir.chn, vir.tgt, vir.lun); 6702dbcdf33SNicholas Bellinger return ERR_PTR(-ENODEV); 671d9d660f6SJuergen Gross } 6722dbcdf33SNicholas Bellinger 6732dbcdf33SNicholas Bellinger pending_req = scsiback_get_pend_req(ring, v2p); 6742dbcdf33SNicholas Bellinger if (IS_ERR(pending_req)) { 6752dbcdf33SNicholas Bellinger kref_put(&v2p->kref, scsiback_free_translation_entry); 6762dbcdf33SNicholas Bellinger return ERR_PTR(-ENOMEM); 6772dbcdf33SNicholas Bellinger } 6782dbcdf33SNicholas Bellinger pending_req->rqid = ring_req->rqid; 6792dbcdf33SNicholas Bellinger pending_req->info = info; 680d9d660f6SJuergen Gross pending_req->v2p = v2p; 681d9d660f6SJuergen Gross pending_req->sc_data_direction = ring_req->sc_data_direction; 682d9d660f6SJuergen Gross pending_req->cmd_len = ring_req->cmd_len; 683d9d660f6SJuergen Gross memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len); 684d9d660f6SJuergen Gross 6852dbcdf33SNicholas Bellinger return pending_req; 686d9d660f6SJuergen Gross } 687d9d660f6SJuergen Gross 68886991b6eSJuergen Gross static int scsiback_do_cmd_fn(struct vscsibk_info *info, 68986991b6eSJuergen Gross unsigned int *eoi_flags) 690d9d660f6SJuergen Gross { 691d9d660f6SJuergen Gross struct vscsiif_back_ring *ring = &info->ring; 692facb5732SJuergen Gross struct vscsiif_request ring_req; 693d9d660f6SJuergen Gross struct vscsibk_pend *pending_req; 694d9d660f6SJuergen Gross RING_IDX rc, rp; 6952dbcdf33SNicholas Bellinger int more_to_do; 696d9d660f6SJuergen Gross uint32_t result; 697d9d660f6SJuergen Gross 698d9d660f6SJuergen Gross rc = ring->req_cons; 699d9d660f6SJuergen Gross rp = ring->sring->req_prod; 700d9d660f6SJuergen Gross rmb(); /* guest system is accessing ring, too */ 701d9d660f6SJuergen Gross 702d9d660f6SJuergen Gross if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) { 703d9d660f6SJuergen Gross rc = ring->rsp_prod_pvt; 70478574878STao Chen pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n", 705d9d660f6SJuergen Gross info->domid, rp, rc, rp - rc); 70686991b6eSJuergen Gross return -EINVAL; 707d9d660f6SJuergen Gross } 708d9d660f6SJuergen Gross 709d9d660f6SJuergen Gross while ((rc != rp)) { 71086991b6eSJuergen Gross *eoi_flags &= ~XEN_EOI_FLAG_SPURIOUS; 71186991b6eSJuergen Gross 712d9d660f6SJuergen Gross if (RING_REQUEST_CONS_OVERFLOW(ring, rc)) 713d9d660f6SJuergen Gross break; 714d9d660f6SJuergen Gross 715be69746eSDavid Vrabel RING_COPY_REQUEST(ring, rc, &ring_req); 716d9d660f6SJuergen Gross ring->req_cons = ++rc; 717d9d660f6SJuergen Gross 7182dbcdf33SNicholas Bellinger pending_req = prepare_pending_reqs(info, ring, &ring_req); 7192dbcdf33SNicholas Bellinger if (IS_ERR(pending_req)) { 7202dbcdf33SNicholas Bellinger switch (PTR_ERR(pending_req)) { 721d9d660f6SJuergen Gross case -ENODEV: 722d9d660f6SJuergen Gross result = DID_NO_CONNECT; 723d9d660f6SJuergen Gross break; 724d9d660f6SJuergen Gross default: 725d9d660f6SJuergen Gross result = DRIVER_ERROR; 726d9d660f6SJuergen Gross break; 727d9d660f6SJuergen Gross } 7282dbcdf33SNicholas Bellinger scsiback_send_response(info, NULL, result << 24, 0, 7292dbcdf33SNicholas Bellinger ring_req.rqid); 730d9d660f6SJuergen Gross return 1; 731d9d660f6SJuergen Gross } 732d9d660f6SJuergen Gross 733facb5732SJuergen Gross switch (ring_req.act) { 734d9d660f6SJuergen Gross case VSCSIIF_ACT_SCSI_CDB: 735facb5732SJuergen Gross if (scsiback_gnttab_data_map(&ring_req, pending_req)) { 736d9d660f6SJuergen Gross scsiback_fast_flush_area(pending_req); 737d9d660f6SJuergen Gross scsiback_do_resp_with_sense(NULL, 738d9d660f6SJuergen Gross DRIVER_ERROR << 24, 0, pending_req); 7392dbcdf33SNicholas Bellinger transport_generic_free_cmd(&pending_req->se_cmd, 0); 740d9d660f6SJuergen Gross } else { 741d9d660f6SJuergen Gross scsiback_cmd_exec(pending_req); 742d9d660f6SJuergen Gross } 743d9d660f6SJuergen Gross break; 744d9d660f6SJuergen Gross case VSCSIIF_ACT_SCSI_ABORT: 745d9d660f6SJuergen Gross scsiback_device_action(pending_req, TMR_ABORT_TASK, 746facb5732SJuergen Gross ring_req.ref_rqid); 747d9d660f6SJuergen Gross break; 748d9d660f6SJuergen Gross case VSCSIIF_ACT_SCSI_RESET: 749d9d660f6SJuergen Gross scsiback_device_action(pending_req, TMR_LUN_RESET, 0); 750d9d660f6SJuergen Gross break; 751d9d660f6SJuergen Gross default: 75278574878STao Chen pr_err_ratelimited("invalid request\n"); 7532dbcdf33SNicholas Bellinger scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0, 7542dbcdf33SNicholas Bellinger pending_req); 7552dbcdf33SNicholas Bellinger transport_generic_free_cmd(&pending_req->se_cmd, 0); 756d9d660f6SJuergen Gross break; 757d9d660f6SJuergen Gross } 758d9d660f6SJuergen Gross 759d9d660f6SJuergen Gross /* Yield point for this unbounded loop. */ 760d9d660f6SJuergen Gross cond_resched(); 761d9d660f6SJuergen Gross } 762d9d660f6SJuergen Gross 763ca33479cSJuergen Gross gnttab_page_cache_shrink(&info->free_pages, scsiback_max_buffer_pages); 764ca33479cSJuergen Gross 765d9d660f6SJuergen Gross RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do); 766d9d660f6SJuergen Gross return more_to_do; 767d9d660f6SJuergen Gross } 768d9d660f6SJuergen Gross 769d9d660f6SJuergen Gross static irqreturn_t scsiback_irq_fn(int irq, void *dev_id) 770d9d660f6SJuergen Gross { 771d9d660f6SJuergen Gross struct vscsibk_info *info = dev_id; 77286991b6eSJuergen Gross int rc; 77386991b6eSJuergen Gross unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS; 774d9d660f6SJuergen Gross 77586991b6eSJuergen Gross while ((rc = scsiback_do_cmd_fn(info, &eoi_flags)) > 0) 776d9d660f6SJuergen Gross cond_resched(); 777d9d660f6SJuergen Gross 77886991b6eSJuergen Gross /* In case of a ring error we keep the event channel masked. */ 77986991b6eSJuergen Gross if (!rc) 78086991b6eSJuergen Gross xen_irq_lateeoi(irq, eoi_flags); 78186991b6eSJuergen Gross 782d9d660f6SJuergen Gross return IRQ_HANDLED; 783d9d660f6SJuergen Gross } 784d9d660f6SJuergen Gross 785d9d660f6SJuergen Gross static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref, 786d9d660f6SJuergen Gross evtchn_port_t evtchn) 787d9d660f6SJuergen Gross { 788d9d660f6SJuergen Gross void *area; 789d9d660f6SJuergen Gross struct vscsiif_sring *sring; 790d9d660f6SJuergen Gross int err; 791d9d660f6SJuergen Gross 792d9d660f6SJuergen Gross if (info->irq) 793d9d660f6SJuergen Gross return -1; 794d9d660f6SJuergen Gross 795ccc9d90aSWei Liu err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area); 796d9d660f6SJuergen Gross if (err) 797d9d660f6SJuergen Gross return err; 798d9d660f6SJuergen Gross 799d9d660f6SJuergen Gross sring = (struct vscsiif_sring *)area; 800d9d660f6SJuergen Gross BACK_RING_INIT(&info->ring, sring, PAGE_SIZE); 801d9d660f6SJuergen Gross 802*f2fa0e5eSJuergen Gross err = bind_interdomain_evtchn_to_irq_lateeoi(info->dev, evtchn); 803d9d660f6SJuergen Gross if (err < 0) 804d9d660f6SJuergen Gross goto unmap_page; 805d9d660f6SJuergen Gross 806d9d660f6SJuergen Gross info->irq = err; 807d9d660f6SJuergen Gross 808d9d660f6SJuergen Gross err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn, 809d9d660f6SJuergen Gross IRQF_ONESHOT, "vscsiif-backend", info); 810d9d660f6SJuergen Gross if (err) 811d9d660f6SJuergen Gross goto free_irq; 812d9d660f6SJuergen Gross 813d9d660f6SJuergen Gross return 0; 814d9d660f6SJuergen Gross 815d9d660f6SJuergen Gross free_irq: 816d9d660f6SJuergen Gross unbind_from_irqhandler(info->irq, info); 817d9d660f6SJuergen Gross info->irq = 0; 818d9d660f6SJuergen Gross unmap_page: 819d9d660f6SJuergen Gross xenbus_unmap_ring_vfree(info->dev, area); 820d9d660f6SJuergen Gross 821d9d660f6SJuergen Gross return err; 822d9d660f6SJuergen Gross } 823d9d660f6SJuergen Gross 824d9d660f6SJuergen Gross static int scsiback_map(struct vscsibk_info *info) 825d9d660f6SJuergen Gross { 826d9d660f6SJuergen Gross struct xenbus_device *dev = info->dev; 8270102e4efSYan Yankovskyi unsigned int ring_ref; 8280102e4efSYan Yankovskyi evtchn_port_t evtchn; 829d9d660f6SJuergen Gross int err; 830d9d660f6SJuergen Gross 831d9d660f6SJuergen Gross err = xenbus_gather(XBT_NIL, dev->otherend, 832d9d660f6SJuergen Gross "ring-ref", "%u", &ring_ref, 833d9d660f6SJuergen Gross "event-channel", "%u", &evtchn, NULL); 834d9d660f6SJuergen Gross if (err) { 835d9d660f6SJuergen Gross xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend); 836d9d660f6SJuergen Gross return err; 837d9d660f6SJuergen Gross } 838d9d660f6SJuergen Gross 839d9d660f6SJuergen Gross return scsiback_init_sring(info, ring_ref, evtchn); 840d9d660f6SJuergen Gross } 841d9d660f6SJuergen Gross 842d9d660f6SJuergen Gross /* 843c9e2f531SJuergen Gross Check for a translation entry being present 844c9e2f531SJuergen Gross */ 845c9e2f531SJuergen Gross static struct v2p_entry *scsiback_chk_translation_entry( 846c9e2f531SJuergen Gross struct vscsibk_info *info, struct ids_tuple *v) 847c9e2f531SJuergen Gross { 848c9e2f531SJuergen Gross struct list_head *head = &(info->v2p_entry_lists); 849c9e2f531SJuergen Gross struct v2p_entry *entry; 850c9e2f531SJuergen Gross 851c9e2f531SJuergen Gross list_for_each_entry(entry, head, l) 852c9e2f531SJuergen Gross if ((entry->v.chn == v->chn) && 853c9e2f531SJuergen Gross (entry->v.tgt == v->tgt) && 854c9e2f531SJuergen Gross (entry->v.lun == v->lun)) 855c9e2f531SJuergen Gross return entry; 856c9e2f531SJuergen Gross 857c9e2f531SJuergen Gross return NULL; 858c9e2f531SJuergen Gross } 859c9e2f531SJuergen Gross 860c9e2f531SJuergen Gross /* 861d9d660f6SJuergen Gross Add a new translation entry 862d9d660f6SJuergen Gross */ 863d9d660f6SJuergen Gross static int scsiback_add_translation_entry(struct vscsibk_info *info, 864d9d660f6SJuergen Gross char *phy, struct ids_tuple *v) 865d9d660f6SJuergen Gross { 866d9d660f6SJuergen Gross int err = 0; 867d9d660f6SJuergen Gross struct v2p_entry *new; 868d9d660f6SJuergen Gross unsigned long flags; 869d9d660f6SJuergen Gross char *lunp; 870196e2e2aSHannes Reinecke unsigned long long unpacked_lun; 8716bb82612SNicholas Bellinger struct se_lun *se_lun; 872d9d660f6SJuergen Gross struct scsiback_tpg *tpg_entry, *tpg = NULL; 873d9d660f6SJuergen Gross char *error = "doesn't exist"; 874d9d660f6SJuergen Gross 875d9d660f6SJuergen Gross lunp = strrchr(phy, ':'); 876d9d660f6SJuergen Gross if (!lunp) { 87778574878STao Chen pr_err("illegal format of physical device %s\n", phy); 878d9d660f6SJuergen Gross return -EINVAL; 879d9d660f6SJuergen Gross } 880d9d660f6SJuergen Gross *lunp = 0; 881d9d660f6SJuergen Gross lunp++; 882196e2e2aSHannes Reinecke err = kstrtoull(lunp, 10, &unpacked_lun); 883196e2e2aSHannes Reinecke if (err < 0) { 88478574878STao Chen pr_err("lun number not valid: %s\n", lunp); 885196e2e2aSHannes Reinecke return err; 886d9d660f6SJuergen Gross } 887d9d660f6SJuergen Gross 888d9d660f6SJuergen Gross mutex_lock(&scsiback_mutex); 889d9d660f6SJuergen Gross list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) { 890d9d660f6SJuergen Gross if (!strcmp(phy, tpg_entry->tport->tport_name) || 891d9d660f6SJuergen Gross !strcmp(phy, tpg_entry->param_alias)) { 8926bb82612SNicholas Bellinger mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex); 8936bb82612SNicholas Bellinger hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) { 8946bb82612SNicholas Bellinger if (se_lun->unpacked_lun == unpacked_lun) { 895d9d660f6SJuergen Gross if (!tpg_entry->tpg_nexus) 896d9d660f6SJuergen Gross error = "nexus undefined"; 897d9d660f6SJuergen Gross else 898d9d660f6SJuergen Gross tpg = tpg_entry; 8996bb82612SNicholas Bellinger break; 900d9d660f6SJuergen Gross } 9016bb82612SNicholas Bellinger } 9026bb82612SNicholas Bellinger mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex); 903d9d660f6SJuergen Gross break; 904d9d660f6SJuergen Gross } 905d9d660f6SJuergen Gross } 906d9d660f6SJuergen Gross if (tpg) { 907d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 908d9d660f6SJuergen Gross tpg->tv_tpg_fe_count++; 909d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 910d9d660f6SJuergen Gross } 911d9d660f6SJuergen Gross mutex_unlock(&scsiback_mutex); 912d9d660f6SJuergen Gross 913d9d660f6SJuergen Gross if (!tpg) { 9140fb1f14eSNicholas Bellinger pr_err("%s:%llu %s\n", phy, unpacked_lun, error); 915d9d660f6SJuergen Gross return -ENODEV; 916d9d660f6SJuergen Gross } 917d9d660f6SJuergen Gross 918d9d660f6SJuergen Gross new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL); 919d9d660f6SJuergen Gross if (new == NULL) { 920d9d660f6SJuergen Gross err = -ENOMEM; 921d9d660f6SJuergen Gross goto out_free; 922d9d660f6SJuergen Gross } 923d9d660f6SJuergen Gross 924d9d660f6SJuergen Gross spin_lock_irqsave(&info->v2p_lock, flags); 925d9d660f6SJuergen Gross 926d9d660f6SJuergen Gross /* Check double assignment to identical virtual ID */ 927c9e2f531SJuergen Gross if (scsiback_chk_translation_entry(info, v)) { 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 /* Create a new translation entry and add to the list */ 934d9d660f6SJuergen Gross kref_init(&new->kref); 935d9d660f6SJuergen Gross new->v = *v; 936d9d660f6SJuergen Gross new->tpg = tpg; 9376bb82612SNicholas Bellinger new->lun = unpacked_lun; 938c9e2f531SJuergen Gross list_add_tail(&new->l, &info->v2p_entry_lists); 939d9d660f6SJuergen Gross 940d9d660f6SJuergen Gross out: 941d9d660f6SJuergen Gross spin_unlock_irqrestore(&info->v2p_lock, flags); 942d9d660f6SJuergen Gross 943d9d660f6SJuergen Gross out_free: 944f285aa8dSJuergen Gross if (err) { 945d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 946d9d660f6SJuergen Gross tpg->tv_tpg_fe_count--; 947d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 948d9d660f6SJuergen Gross kfree(new); 949f285aa8dSJuergen Gross } 950d9d660f6SJuergen Gross 951d9d660f6SJuergen Gross return err; 952d9d660f6SJuergen Gross } 953d9d660f6SJuergen Gross 954d9d660f6SJuergen Gross static void __scsiback_del_translation_entry(struct v2p_entry *entry) 955d9d660f6SJuergen Gross { 956d9d660f6SJuergen Gross list_del(&entry->l); 957d9d660f6SJuergen Gross kref_put(&entry->kref, scsiback_free_translation_entry); 958d9d660f6SJuergen Gross } 959d9d660f6SJuergen Gross 960d9d660f6SJuergen Gross /* 961c9e2f531SJuergen Gross Delete the translation entry specified 962d9d660f6SJuergen Gross */ 963d9d660f6SJuergen Gross static int scsiback_del_translation_entry(struct vscsibk_info *info, 964d9d660f6SJuergen Gross struct ids_tuple *v) 965d9d660f6SJuergen Gross { 966d9d660f6SJuergen Gross struct v2p_entry *entry; 967d9d660f6SJuergen Gross unsigned long flags; 968c9e2f531SJuergen Gross int ret = 0; 969d9d660f6SJuergen Gross 970d9d660f6SJuergen Gross spin_lock_irqsave(&info->v2p_lock, flags); 971d9d660f6SJuergen Gross /* Find out the translation entry specified */ 972c9e2f531SJuergen Gross entry = scsiback_chk_translation_entry(info, v); 973c9e2f531SJuergen Gross if (entry) 974d9d660f6SJuergen Gross __scsiback_del_translation_entry(entry); 975c9e2f531SJuergen Gross else 976c9e2f531SJuergen Gross ret = -ENOENT; 977d9d660f6SJuergen Gross 978d9d660f6SJuergen Gross spin_unlock_irqrestore(&info->v2p_lock, flags); 979c9e2f531SJuergen Gross return ret; 980d9d660f6SJuergen Gross } 981d9d660f6SJuergen Gross 982d9d660f6SJuergen Gross static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state, 983169e6cf0SJuergen Gross char *phy, struct ids_tuple *vir, int try) 984d9d660f6SJuergen Gross { 985c9e2f531SJuergen Gross struct v2p_entry *entry; 986c9e2f531SJuergen Gross unsigned long flags; 9877c63ca24SZhouyang Jia int err; 988c9e2f531SJuergen Gross 989c9e2f531SJuergen Gross if (try) { 990c9e2f531SJuergen Gross spin_lock_irqsave(&info->v2p_lock, flags); 991c9e2f531SJuergen Gross entry = scsiback_chk_translation_entry(info, vir); 992c9e2f531SJuergen Gross spin_unlock_irqrestore(&info->v2p_lock, flags); 993c9e2f531SJuergen Gross if (entry) 994c9e2f531SJuergen Gross return; 995c9e2f531SJuergen 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) { 10037c63ca24SZhouyang Jia err = xenbus_printf(XBT_NIL, info->dev->nodename, state, 1004d9d660f6SJuergen Gross "%d", XenbusStateClosed); 10057c63ca24SZhouyang Jia if (err) 10067c63ca24SZhouyang Jia xenbus_dev_error(info->dev, err, 10077c63ca24SZhouyang Jia "%s: writing %s", __func__, state); 1008d9d660f6SJuergen Gross } 1009d9d660f6SJuergen Gross } 1010d9d660f6SJuergen Gross 1011d9d660f6SJuergen Gross static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state, 1012d9d660f6SJuergen Gross struct ids_tuple *vir) 1013d9d660f6SJuergen Gross { 1014d9d660f6SJuergen Gross if (!scsiback_del_translation_entry(info, vir)) { 1015d9d660f6SJuergen Gross if (xenbus_printf(XBT_NIL, info->dev->nodename, state, 1016d9d660f6SJuergen Gross "%d", XenbusStateClosed)) 101778574878STao Chen pr_err("xenbus_printf error %s\n", state); 1018d9d660f6SJuergen Gross } 1019d9d660f6SJuergen Gross } 1020d9d660f6SJuergen Gross 1021d9d660f6SJuergen Gross #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1 1022d9d660f6SJuergen Gross #define VSCSIBACK_OP_UPDATEDEV_STATE 2 1023d9d660f6SJuergen Gross 1024d9d660f6SJuergen Gross static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op, 1025d9d660f6SJuergen Gross char *ent) 1026d9d660f6SJuergen Gross { 1027d9d660f6SJuergen Gross int err; 1028d9d660f6SJuergen Gross struct ids_tuple vir; 1029d9d660f6SJuergen Gross char *val; 1030d9d660f6SJuergen Gross int device_state; 1031d9d660f6SJuergen Gross char phy[VSCSI_NAMELEN]; 1032d9d660f6SJuergen Gross char str[64]; 1033d9d660f6SJuergen Gross char state[64]; 1034d9d660f6SJuergen Gross struct xenbus_device *dev = info->dev; 1035d9d660f6SJuergen Gross 1036d9d660f6SJuergen Gross /* read status */ 1037d9d660f6SJuergen Gross snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent); 1038d9d660f6SJuergen Gross err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state); 1039d9d660f6SJuergen Gross if (XENBUS_EXIST_ERR(err)) 1040d9d660f6SJuergen Gross return; 1041d9d660f6SJuergen Gross 1042d9d660f6SJuergen Gross /* physical SCSI device */ 1043d9d660f6SJuergen Gross snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent); 1044d9d660f6SJuergen Gross val = xenbus_read(XBT_NIL, dev->nodename, str, NULL); 1045d9d660f6SJuergen Gross if (IS_ERR(val)) { 10467c63ca24SZhouyang Jia err = xenbus_printf(XBT_NIL, dev->nodename, state, 1047d9d660f6SJuergen Gross "%d", XenbusStateClosed); 10487c63ca24SZhouyang Jia if (err) 10497c63ca24SZhouyang Jia xenbus_dev_error(info->dev, err, 10507c63ca24SZhouyang Jia "%s: writing %s", __func__, state); 1051d9d660f6SJuergen Gross return; 1052d9d660f6SJuergen Gross } 1053d9d660f6SJuergen Gross strlcpy(phy, val, VSCSI_NAMELEN); 1054d9d660f6SJuergen Gross kfree(val); 1055d9d660f6SJuergen Gross 1056d9d660f6SJuergen Gross /* virtual SCSI device */ 1057d9d660f6SJuergen Gross snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent); 1058d9d660f6SJuergen Gross err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u", 1059d9d660f6SJuergen Gross &vir.hst, &vir.chn, &vir.tgt, &vir.lun); 1060d9d660f6SJuergen Gross if (XENBUS_EXIST_ERR(err)) { 10617c63ca24SZhouyang Jia err = xenbus_printf(XBT_NIL, dev->nodename, state, 1062d9d660f6SJuergen Gross "%d", XenbusStateClosed); 10637c63ca24SZhouyang Jia if (err) 10647c63ca24SZhouyang Jia xenbus_dev_error(info->dev, err, 10657c63ca24SZhouyang Jia "%s: writing %s", __func__, state); 1066d9d660f6SJuergen Gross return; 1067d9d660f6SJuergen Gross } 1068d9d660f6SJuergen Gross 1069d9d660f6SJuergen Gross switch (op) { 1070d9d660f6SJuergen Gross case VSCSIBACK_OP_ADD_OR_DEL_LUN: 1071169e6cf0SJuergen Gross switch (device_state) { 1072169e6cf0SJuergen Gross case XenbusStateInitialising: 1073169e6cf0SJuergen Gross scsiback_do_add_lun(info, state, phy, &vir, 0); 1074169e6cf0SJuergen Gross break; 1075169e6cf0SJuergen Gross case XenbusStateConnected: 1076169e6cf0SJuergen Gross scsiback_do_add_lun(info, state, phy, &vir, 1); 1077169e6cf0SJuergen Gross break; 1078169e6cf0SJuergen Gross case XenbusStateClosing: 1079d9d660f6SJuergen Gross scsiback_do_del_lun(info, state, &vir); 1080d9d660f6SJuergen Gross break; 1081169e6cf0SJuergen Gross default: 1082169e6cf0SJuergen Gross break; 1083169e6cf0SJuergen Gross } 1084169e6cf0SJuergen Gross break; 1085d9d660f6SJuergen Gross 1086d9d660f6SJuergen Gross case VSCSIBACK_OP_UPDATEDEV_STATE: 1087d9d660f6SJuergen Gross if (device_state == XenbusStateInitialised) { 1088d9d660f6SJuergen Gross /* modify vscsi-devs/dev-x/state */ 1089d9d660f6SJuergen Gross if (xenbus_printf(XBT_NIL, dev->nodename, state, 1090d9d660f6SJuergen Gross "%d", XenbusStateConnected)) { 109178574878STao Chen pr_err("xenbus_printf error %s\n", str); 1092d9d660f6SJuergen Gross scsiback_del_translation_entry(info, &vir); 1093d9d660f6SJuergen Gross xenbus_printf(XBT_NIL, dev->nodename, state, 1094d9d660f6SJuergen Gross "%d", XenbusStateClosed); 1095d9d660f6SJuergen Gross } 1096d9d660f6SJuergen Gross } 1097d9d660f6SJuergen Gross break; 1098d9d660f6SJuergen Gross /* When it is necessary, processing is added here. */ 1099d9d660f6SJuergen Gross default: 1100d9d660f6SJuergen Gross break; 1101d9d660f6SJuergen Gross } 1102d9d660f6SJuergen Gross } 1103d9d660f6SJuergen Gross 1104d9d660f6SJuergen Gross static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op) 1105d9d660f6SJuergen Gross { 1106d9d660f6SJuergen Gross int i; 1107d9d660f6SJuergen Gross char **dir; 1108d9d660f6SJuergen Gross unsigned int ndir = 0; 1109d9d660f6SJuergen Gross 1110d9d660f6SJuergen Gross dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs", 1111d9d660f6SJuergen Gross &ndir); 1112d9d660f6SJuergen Gross if (IS_ERR(dir)) 1113d9d660f6SJuergen Gross return; 1114d9d660f6SJuergen Gross 1115d9d660f6SJuergen Gross for (i = 0; i < ndir; i++) 1116d9d660f6SJuergen Gross scsiback_do_1lun_hotplug(info, op, dir[i]); 1117d9d660f6SJuergen Gross 1118d9d660f6SJuergen Gross kfree(dir); 1119d9d660f6SJuergen Gross } 1120d9d660f6SJuergen Gross 1121d9d660f6SJuergen Gross static void scsiback_frontend_changed(struct xenbus_device *dev, 1122d9d660f6SJuergen Gross enum xenbus_state frontend_state) 1123d9d660f6SJuergen Gross { 1124d9d660f6SJuergen Gross struct vscsibk_info *info = dev_get_drvdata(&dev->dev); 1125d9d660f6SJuergen Gross 1126d9d660f6SJuergen Gross switch (frontend_state) { 1127d9d660f6SJuergen Gross case XenbusStateInitialising: 1128d9d660f6SJuergen Gross break; 1129d9d660f6SJuergen Gross 1130d9d660f6SJuergen Gross case XenbusStateInitialised: 1131d9d660f6SJuergen Gross if (scsiback_map(info)) 1132d9d660f6SJuergen Gross break; 1133d9d660f6SJuergen Gross 1134d9d660f6SJuergen Gross scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN); 1135d9d660f6SJuergen Gross xenbus_switch_state(dev, XenbusStateConnected); 1136d9d660f6SJuergen Gross break; 1137d9d660f6SJuergen Gross 1138d9d660f6SJuergen Gross case XenbusStateConnected: 1139d9d660f6SJuergen Gross scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE); 1140d9d660f6SJuergen Gross 1141d9d660f6SJuergen Gross if (dev->state == XenbusStateConnected) 1142d9d660f6SJuergen Gross break; 1143d9d660f6SJuergen Gross 1144d9d660f6SJuergen Gross xenbus_switch_state(dev, XenbusStateConnected); 1145d9d660f6SJuergen Gross break; 1146d9d660f6SJuergen Gross 1147d9d660f6SJuergen Gross case XenbusStateClosing: 1148d9d660f6SJuergen Gross if (info->irq) 1149d9d660f6SJuergen Gross scsiback_disconnect(info); 1150d9d660f6SJuergen Gross 1151d9d660f6SJuergen Gross xenbus_switch_state(dev, XenbusStateClosing); 1152d9d660f6SJuergen Gross break; 1153d9d660f6SJuergen Gross 1154d9d660f6SJuergen Gross case XenbusStateClosed: 1155d9d660f6SJuergen Gross xenbus_switch_state(dev, XenbusStateClosed); 1156d9d660f6SJuergen Gross if (xenbus_dev_is_online(dev)) 1157d9d660f6SJuergen Gross break; 1158df561f66SGustavo A. R. Silva fallthrough; /* if not online */ 1159d9d660f6SJuergen Gross case XenbusStateUnknown: 1160d9d660f6SJuergen Gross device_unregister(&dev->dev); 1161d9d660f6SJuergen Gross break; 1162d9d660f6SJuergen Gross 1163d9d660f6SJuergen Gross case XenbusStateReconfiguring: 1164d9d660f6SJuergen Gross scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN); 1165d9d660f6SJuergen Gross xenbus_switch_state(dev, XenbusStateReconfigured); 1166d9d660f6SJuergen Gross 1167d9d660f6SJuergen Gross break; 1168d9d660f6SJuergen Gross 1169d9d660f6SJuergen Gross default: 1170d9d660f6SJuergen Gross xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", 1171d9d660f6SJuergen Gross frontend_state); 1172d9d660f6SJuergen Gross break; 1173d9d660f6SJuergen Gross } 1174d9d660f6SJuergen Gross } 1175d9d660f6SJuergen Gross 1176d9d660f6SJuergen Gross /* 1177d9d660f6SJuergen Gross Release the translation entry specfied 1178d9d660f6SJuergen Gross */ 1179d9d660f6SJuergen Gross static void scsiback_release_translation_entry(struct vscsibk_info *info) 1180d9d660f6SJuergen Gross { 1181d9d660f6SJuergen Gross struct v2p_entry *entry, *tmp; 1182d9d660f6SJuergen Gross struct list_head *head = &(info->v2p_entry_lists); 1183d9d660f6SJuergen Gross unsigned long flags; 1184d9d660f6SJuergen Gross 1185d9d660f6SJuergen Gross spin_lock_irqsave(&info->v2p_lock, flags); 1186d9d660f6SJuergen Gross 1187d9d660f6SJuergen Gross list_for_each_entry_safe(entry, tmp, head, l) 1188d9d660f6SJuergen Gross __scsiback_del_translation_entry(entry); 1189d9d660f6SJuergen Gross 1190d9d660f6SJuergen Gross spin_unlock_irqrestore(&info->v2p_lock, flags); 1191d9d660f6SJuergen Gross } 1192d9d660f6SJuergen Gross 1193d9d660f6SJuergen Gross static int scsiback_remove(struct xenbus_device *dev) 1194d9d660f6SJuergen Gross { 1195d9d660f6SJuergen Gross struct vscsibk_info *info = dev_get_drvdata(&dev->dev); 1196d9d660f6SJuergen Gross 1197d9d660f6SJuergen Gross if (info->irq) 1198d9d660f6SJuergen Gross scsiback_disconnect(info); 1199d9d660f6SJuergen Gross 1200d9d660f6SJuergen Gross scsiback_release_translation_entry(info); 1201d9d660f6SJuergen Gross 1202ca33479cSJuergen Gross gnttab_page_cache_shrink(&info->free_pages, 0); 1203ca33479cSJuergen Gross 1204d9d660f6SJuergen Gross dev_set_drvdata(&dev->dev, NULL); 1205d9d660f6SJuergen Gross 1206d9d660f6SJuergen Gross return 0; 1207d9d660f6SJuergen Gross } 1208d9d660f6SJuergen Gross 1209d9d660f6SJuergen Gross static int scsiback_probe(struct xenbus_device *dev, 1210d9d660f6SJuergen Gross const struct xenbus_device_id *id) 1211d9d660f6SJuergen Gross { 1212d9d660f6SJuergen Gross int err; 1213d9d660f6SJuergen Gross 1214d9d660f6SJuergen Gross struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info), 1215d9d660f6SJuergen Gross GFP_KERNEL); 1216d9d660f6SJuergen Gross 121778574878STao Chen pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 1218d9d660f6SJuergen Gross 1219d9d660f6SJuergen Gross if (!info) { 1220d9d660f6SJuergen Gross xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure"); 1221d9d660f6SJuergen Gross return -ENOMEM; 1222d9d660f6SJuergen Gross } 1223d9d660f6SJuergen Gross info->dev = dev; 1224d9d660f6SJuergen Gross dev_set_drvdata(&dev->dev, info); 1225d9d660f6SJuergen Gross 1226d9d660f6SJuergen Gross info->domid = dev->otherend_id; 1227d9d660f6SJuergen Gross spin_lock_init(&info->ring_lock); 1228d9d660f6SJuergen Gross atomic_set(&info->nr_unreplied_reqs, 0); 1229d9d660f6SJuergen Gross init_waitqueue_head(&info->waiting_to_free); 1230d9d660f6SJuergen Gross info->dev = dev; 1231d9d660f6SJuergen Gross info->irq = 0; 1232d9d660f6SJuergen Gross INIT_LIST_HEAD(&info->v2p_entry_lists); 1233d9d660f6SJuergen Gross spin_lock_init(&info->v2p_lock); 1234ca33479cSJuergen Gross gnttab_page_cache_init(&info->free_pages); 1235d9d660f6SJuergen Gross 1236d9d660f6SJuergen Gross err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u", 1237d9d660f6SJuergen Gross SG_ALL); 1238d9d660f6SJuergen Gross if (err) 1239d9d660f6SJuergen Gross xenbus_dev_error(dev, err, "writing feature-sg-grant"); 1240d9d660f6SJuergen Gross 1241d9d660f6SJuergen Gross err = xenbus_switch_state(dev, XenbusStateInitWait); 1242d9d660f6SJuergen Gross if (err) 1243d9d660f6SJuergen Gross goto fail; 1244d9d660f6SJuergen Gross 1245d9d660f6SJuergen Gross return 0; 1246d9d660f6SJuergen Gross 1247d9d660f6SJuergen Gross fail: 124878574878STao Chen pr_warn("%s failed\n", __func__); 1249d9d660f6SJuergen Gross scsiback_remove(dev); 1250d9d660f6SJuergen Gross 1251d9d660f6SJuergen Gross return err; 1252d9d660f6SJuergen Gross } 1253d9d660f6SJuergen Gross 1254d9d660f6SJuergen Gross static char *scsiback_dump_proto_id(struct scsiback_tport *tport) 1255d9d660f6SJuergen Gross { 1256d9d660f6SJuergen Gross switch (tport->tport_proto_id) { 1257d9d660f6SJuergen Gross case SCSI_PROTOCOL_SAS: 1258d9d660f6SJuergen Gross return "SAS"; 1259d9d660f6SJuergen Gross case SCSI_PROTOCOL_FCP: 1260d9d660f6SJuergen Gross return "FCP"; 1261d9d660f6SJuergen Gross case SCSI_PROTOCOL_ISCSI: 1262d9d660f6SJuergen Gross return "iSCSI"; 1263d9d660f6SJuergen Gross default: 1264d9d660f6SJuergen Gross break; 1265d9d660f6SJuergen Gross } 1266d9d660f6SJuergen Gross 1267d9d660f6SJuergen Gross return "Unknown"; 1268d9d660f6SJuergen Gross } 1269d9d660f6SJuergen Gross 1270d9d660f6SJuergen Gross static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg) 1271d9d660f6SJuergen Gross { 1272d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1273d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1274d9d660f6SJuergen Gross struct scsiback_tport *tport = tpg->tport; 1275d9d660f6SJuergen Gross 1276d9d660f6SJuergen Gross return &tport->tport_name[0]; 1277d9d660f6SJuergen Gross } 1278d9d660f6SJuergen Gross 1279d9d660f6SJuergen Gross static u16 scsiback_get_tag(struct se_portal_group *se_tpg) 1280d9d660f6SJuergen Gross { 1281d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1282d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1283d9d660f6SJuergen Gross return tpg->tport_tpgt; 1284d9d660f6SJuergen Gross } 1285d9d660f6SJuergen Gross 1286d9d660f6SJuergen Gross static struct se_wwn * 1287d9d660f6SJuergen Gross scsiback_make_tport(struct target_fabric_configfs *tf, 1288d9d660f6SJuergen Gross struct config_group *group, 1289d9d660f6SJuergen Gross const char *name) 1290d9d660f6SJuergen Gross { 1291d9d660f6SJuergen Gross struct scsiback_tport *tport; 1292d9d660f6SJuergen Gross char *ptr; 1293d9d660f6SJuergen Gross u64 wwpn = 0; 1294d9d660f6SJuergen Gross int off = 0; 1295d9d660f6SJuergen Gross 1296d9d660f6SJuergen Gross tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL); 1297d9d660f6SJuergen Gross if (!tport) 1298d9d660f6SJuergen Gross return ERR_PTR(-ENOMEM); 1299d9d660f6SJuergen Gross 1300d9d660f6SJuergen Gross tport->tport_wwpn = wwpn; 1301d9d660f6SJuergen Gross /* 1302d9d660f6SJuergen Gross * Determine the emulated Protocol Identifier and Target Port Name 1303d9d660f6SJuergen Gross * based on the incoming configfs directory name. 1304d9d660f6SJuergen Gross */ 1305d9d660f6SJuergen Gross ptr = strstr(name, "naa."); 1306d9d660f6SJuergen Gross if (ptr) { 1307d9d660f6SJuergen Gross tport->tport_proto_id = SCSI_PROTOCOL_SAS; 1308d9d660f6SJuergen Gross goto check_len; 1309d9d660f6SJuergen Gross } 1310d9d660f6SJuergen Gross ptr = strstr(name, "fc."); 1311d9d660f6SJuergen Gross if (ptr) { 1312d9d660f6SJuergen Gross tport->tport_proto_id = SCSI_PROTOCOL_FCP; 1313d9d660f6SJuergen Gross off = 3; /* Skip over "fc." */ 1314d9d660f6SJuergen Gross goto check_len; 1315d9d660f6SJuergen Gross } 1316d9d660f6SJuergen Gross ptr = strstr(name, "iqn."); 1317d9d660f6SJuergen Gross if (ptr) { 1318d9d660f6SJuergen Gross tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; 1319d9d660f6SJuergen Gross goto check_len; 1320d9d660f6SJuergen Gross } 1321d9d660f6SJuergen Gross 1322d9d660f6SJuergen Gross pr_err("Unable to locate prefix for emulated Target Port: %s\n", name); 1323d9d660f6SJuergen Gross kfree(tport); 1324d9d660f6SJuergen Gross return ERR_PTR(-EINVAL); 1325d9d660f6SJuergen Gross 1326d9d660f6SJuergen Gross check_len: 1327d9d660f6SJuergen Gross if (strlen(name) >= VSCSI_NAMELEN) { 1328d9d660f6SJuergen Gross pr_err("Emulated %s Address: %s, exceeds max: %d\n", name, 1329d9d660f6SJuergen Gross scsiback_dump_proto_id(tport), VSCSI_NAMELEN); 1330d9d660f6SJuergen Gross kfree(tport); 1331d9d660f6SJuergen Gross return ERR_PTR(-EINVAL); 1332d9d660f6SJuergen Gross } 1333d9d660f6SJuergen Gross snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]); 1334d9d660f6SJuergen Gross 133578574878STao Chen pr_debug("Allocated emulated Target %s Address: %s\n", 1336d9d660f6SJuergen Gross scsiback_dump_proto_id(tport), name); 1337d9d660f6SJuergen Gross 1338d9d660f6SJuergen Gross return &tport->tport_wwn; 1339d9d660f6SJuergen Gross } 1340d9d660f6SJuergen Gross 1341d9d660f6SJuergen Gross static void scsiback_drop_tport(struct se_wwn *wwn) 1342d9d660f6SJuergen Gross { 1343d9d660f6SJuergen Gross struct scsiback_tport *tport = container_of(wwn, 1344d9d660f6SJuergen Gross struct scsiback_tport, tport_wwn); 1345d9d660f6SJuergen Gross 134678574878STao Chen pr_debug("Deallocating emulated Target %s Address: %s\n", 1347d9d660f6SJuergen Gross scsiback_dump_proto_id(tport), tport->tport_name); 1348d9d660f6SJuergen Gross 1349d9d660f6SJuergen Gross kfree(tport); 1350d9d660f6SJuergen Gross } 1351d9d660f6SJuergen Gross 1352d9d660f6SJuergen Gross static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg) 1353d9d660f6SJuergen Gross { 1354d9d660f6SJuergen Gross return 1; 1355d9d660f6SJuergen Gross } 1356d9d660f6SJuergen Gross 1357d9d660f6SJuergen Gross static int scsiback_check_stop_free(struct se_cmd *se_cmd) 1358d9d660f6SJuergen Gross { 1359fa22e7b7SNicholas Bellinger return transport_generic_free_cmd(se_cmd, 0); 1360d9d660f6SJuergen Gross } 1361d9d660f6SJuergen Gross 1362d9d660f6SJuergen Gross static void scsiback_release_cmd(struct se_cmd *se_cmd) 1363d9d660f6SJuergen Gross { 136483c2b54bSMatthew Wilcox target_free_tag(se_cmd->se_sess, se_cmd); 1365d9d660f6SJuergen Gross } 1366d9d660f6SJuergen Gross 1367d9d660f6SJuergen Gross static u32 scsiback_sess_get_index(struct se_session *se_sess) 1368d9d660f6SJuergen Gross { 1369d9d660f6SJuergen Gross return 0; 1370d9d660f6SJuergen Gross } 1371d9d660f6SJuergen Gross 1372d9d660f6SJuergen Gross static int scsiback_write_pending(struct se_cmd *se_cmd) 1373d9d660f6SJuergen Gross { 1374d9d660f6SJuergen Gross /* Go ahead and process the write immediately */ 1375d9d660f6SJuergen Gross target_execute_cmd(se_cmd); 1376d9d660f6SJuergen Gross 1377d9d660f6SJuergen Gross return 0; 1378d9d660f6SJuergen Gross } 1379d9d660f6SJuergen Gross 1380d9d660f6SJuergen Gross static void scsiback_set_default_node_attrs(struct se_node_acl *nacl) 1381d9d660f6SJuergen Gross { 1382d9d660f6SJuergen Gross } 1383d9d660f6SJuergen Gross 1384d9d660f6SJuergen Gross static int scsiback_get_cmd_state(struct se_cmd *se_cmd) 1385d9d660f6SJuergen Gross { 1386d9d660f6SJuergen Gross return 0; 1387d9d660f6SJuergen Gross } 1388d9d660f6SJuergen Gross 1389d9d660f6SJuergen Gross static int scsiback_queue_data_in(struct se_cmd *se_cmd) 1390d9d660f6SJuergen Gross { 1391d9d660f6SJuergen Gross struct vscsibk_pend *pending_req = container_of(se_cmd, 1392d9d660f6SJuergen Gross struct vscsibk_pend, se_cmd); 1393d9d660f6SJuergen Gross 1394d9d660f6SJuergen Gross pending_req->result = SAM_STAT_GOOD; 1395d9d660f6SJuergen Gross scsiback_cmd_done(pending_req); 1396d9d660f6SJuergen Gross return 0; 1397d9d660f6SJuergen Gross } 1398d9d660f6SJuergen Gross 1399d9d660f6SJuergen Gross static int scsiback_queue_status(struct se_cmd *se_cmd) 1400d9d660f6SJuergen Gross { 1401d9d660f6SJuergen Gross struct vscsibk_pend *pending_req = container_of(se_cmd, 1402d9d660f6SJuergen Gross struct vscsibk_pend, se_cmd); 1403d9d660f6SJuergen Gross 1404d9d660f6SJuergen Gross if (se_cmd->sense_buffer && 1405d9d660f6SJuergen Gross ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || 1406d9d660f6SJuergen Gross (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) 1407d9d660f6SJuergen Gross pending_req->result = (DRIVER_SENSE << 24) | 1408d9d660f6SJuergen Gross SAM_STAT_CHECK_CONDITION; 1409d9d660f6SJuergen Gross else 1410d9d660f6SJuergen Gross pending_req->result = se_cmd->scsi_status; 1411d9d660f6SJuergen Gross 1412d9d660f6SJuergen Gross scsiback_cmd_done(pending_req); 1413d9d660f6SJuergen Gross return 0; 1414d9d660f6SJuergen Gross } 1415d9d660f6SJuergen Gross 1416d9d660f6SJuergen Gross static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd) 1417d9d660f6SJuergen Gross { 14189f4ab18aSBart Van Assche struct vscsibk_pend *pending_req = container_of(se_cmd, 14199f4ab18aSBart Van Assche struct vscsibk_pend, se_cmd); 1420d9d660f6SJuergen Gross 1421e3eac124SBart Van Assche complete(&pending_req->tmr_done); 1422d9d660f6SJuergen Gross } 1423d9d660f6SJuergen Gross 1424d9d660f6SJuergen Gross static void scsiback_aborted_task(struct se_cmd *se_cmd) 1425d9d660f6SJuergen Gross { 1426d9d660f6SJuergen Gross } 1427d9d660f6SJuergen Gross 14282eafd729SChristoph Hellwig static ssize_t scsiback_tpg_param_alias_show(struct config_item *item, 1429d9d660f6SJuergen Gross char *page) 1430d9d660f6SJuergen Gross { 14312eafd729SChristoph Hellwig struct se_portal_group *se_tpg = param_to_tpg(item); 1432d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg, 1433d9d660f6SJuergen Gross se_tpg); 1434d9d660f6SJuergen Gross ssize_t rb; 1435d9d660f6SJuergen Gross 1436d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1437d9d660f6SJuergen Gross rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias); 1438d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1439d9d660f6SJuergen Gross 1440d9d660f6SJuergen Gross return rb; 1441d9d660f6SJuergen Gross } 1442d9d660f6SJuergen Gross 14432eafd729SChristoph Hellwig static ssize_t scsiback_tpg_param_alias_store(struct config_item *item, 1444d9d660f6SJuergen Gross const char *page, size_t count) 1445d9d660f6SJuergen Gross { 14462eafd729SChristoph Hellwig struct se_portal_group *se_tpg = param_to_tpg(item); 1447d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg, 1448d9d660f6SJuergen Gross se_tpg); 1449d9d660f6SJuergen Gross int len; 1450d9d660f6SJuergen Gross 1451d9d660f6SJuergen Gross if (strlen(page) >= VSCSI_NAMELEN) { 1452d9d660f6SJuergen Gross pr_err("param alias: %s, exceeds max: %d\n", page, 1453d9d660f6SJuergen Gross VSCSI_NAMELEN); 1454d9d660f6SJuergen Gross return -EINVAL; 1455d9d660f6SJuergen Gross } 1456d9d660f6SJuergen Gross 1457d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1458d9d660f6SJuergen Gross len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page); 1459d9d660f6SJuergen Gross if (tpg->param_alias[len - 1] == '\n') 1460d9d660f6SJuergen Gross tpg->param_alias[len - 1] = '\0'; 1461d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1462d9d660f6SJuergen Gross 1463d9d660f6SJuergen Gross return count; 1464d9d660f6SJuergen Gross } 1465d9d660f6SJuergen Gross 14662eafd729SChristoph Hellwig CONFIGFS_ATTR(scsiback_tpg_param_, alias); 1467d9d660f6SJuergen Gross 1468d9d660f6SJuergen Gross static struct configfs_attribute *scsiback_param_attrs[] = { 14692eafd729SChristoph Hellwig &scsiback_tpg_param_attr_alias, 1470d9d660f6SJuergen Gross NULL, 1471d9d660f6SJuergen Gross }; 1472d9d660f6SJuergen Gross 1473fb444abeSChristoph Hellwig static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg, 1474fb444abeSChristoph Hellwig struct se_session *se_sess, void *p) 1475fb444abeSChristoph Hellwig { 1476fb444abeSChristoph Hellwig struct scsiback_tpg *tpg = container_of(se_tpg, 1477fb444abeSChristoph Hellwig struct scsiback_tpg, se_tpg); 1478fb444abeSChristoph Hellwig 1479fb444abeSChristoph Hellwig tpg->tpg_nexus = p; 1480fb444abeSChristoph Hellwig return 0; 1481fb444abeSChristoph Hellwig } 1482fb444abeSChristoph Hellwig 1483d9d660f6SJuergen Gross static int scsiback_make_nexus(struct scsiback_tpg *tpg, 1484d9d660f6SJuergen Gross const char *name) 1485d9d660f6SJuergen Gross { 1486d9d660f6SJuergen Gross struct scsiback_nexus *tv_nexus; 1487fb444abeSChristoph Hellwig int ret = 0; 1488d9d660f6SJuergen Gross 1489d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1490d9d660f6SJuergen Gross if (tpg->tpg_nexus) { 1491d9d660f6SJuergen Gross pr_debug("tpg->tpg_nexus already exists\n"); 1492fb444abeSChristoph Hellwig ret = -EEXIST; 1493fb444abeSChristoph Hellwig goto out_unlock; 1494d9d660f6SJuergen Gross } 1495d9d660f6SJuergen Gross 1496d9d660f6SJuergen Gross tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL); 1497d9d660f6SJuergen Gross if (!tv_nexus) { 1498fb444abeSChristoph Hellwig ret = -ENOMEM; 1499fb444abeSChristoph Hellwig goto out_unlock; 1500d9d660f6SJuergen Gross } 1501fb444abeSChristoph Hellwig 1502fa834287SMike Christie tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 15032dbcdf33SNicholas Bellinger VSCSI_DEFAULT_SESSION_TAGS, 15042dbcdf33SNicholas Bellinger sizeof(struct vscsibk_pend), 1505fb444abeSChristoph Hellwig TARGET_PROT_NORMAL, name, 1506fb444abeSChristoph Hellwig tv_nexus, scsiback_alloc_sess_cb); 1507d9d660f6SJuergen Gross if (IS_ERR(tv_nexus->tvn_se_sess)) { 1508d9d660f6SJuergen Gross kfree(tv_nexus); 1509fb444abeSChristoph Hellwig ret = -ENOMEM; 1510fb444abeSChristoph Hellwig goto out_unlock; 1511d9d660f6SJuergen Gross } 1512d9d660f6SJuergen Gross 1513fb444abeSChristoph Hellwig out_unlock: 1514d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1515fb444abeSChristoph Hellwig return ret; 1516d9d660f6SJuergen Gross } 1517d9d660f6SJuergen Gross 1518d9d660f6SJuergen Gross static int scsiback_drop_nexus(struct scsiback_tpg *tpg) 1519d9d660f6SJuergen Gross { 1520d9d660f6SJuergen Gross struct se_session *se_sess; 1521d9d660f6SJuergen Gross struct scsiback_nexus *tv_nexus; 1522d9d660f6SJuergen Gross 1523d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1524d9d660f6SJuergen Gross tv_nexus = tpg->tpg_nexus; 1525d9d660f6SJuergen Gross if (!tv_nexus) { 1526d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1527d9d660f6SJuergen Gross return -ENODEV; 1528d9d660f6SJuergen Gross } 1529d9d660f6SJuergen Gross 1530d9d660f6SJuergen Gross se_sess = tv_nexus->tvn_se_sess; 1531d9d660f6SJuergen Gross if (!se_sess) { 1532d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1533d9d660f6SJuergen Gross return -ENODEV; 1534d9d660f6SJuergen Gross } 1535d9d660f6SJuergen Gross 1536d9d660f6SJuergen Gross if (tpg->tv_tpg_port_count != 0) { 1537d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1538d9d660f6SJuergen Gross pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n", 1539d9d660f6SJuergen Gross tpg->tv_tpg_port_count); 1540d9d660f6SJuergen Gross return -EBUSY; 1541d9d660f6SJuergen Gross } 1542d9d660f6SJuergen Gross 1543d9d660f6SJuergen Gross if (tpg->tv_tpg_fe_count != 0) { 1544d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1545d9d660f6SJuergen Gross pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n", 1546d9d660f6SJuergen Gross tpg->tv_tpg_fe_count); 1547d9d660f6SJuergen Gross return -EBUSY; 1548d9d660f6SJuergen Gross } 1549d9d660f6SJuergen Gross 155078574878STao Chen pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n", 1551d9d660f6SJuergen Gross scsiback_dump_proto_id(tpg->tport), 1552d9d660f6SJuergen Gross tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1553d9d660f6SJuergen Gross 1554d9d660f6SJuergen Gross /* 1555d9d660f6SJuergen Gross * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port 1556d9d660f6SJuergen Gross */ 155725b88550SMike Christie target_remove_session(se_sess); 1558d9d660f6SJuergen Gross tpg->tpg_nexus = NULL; 1559d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1560d9d660f6SJuergen Gross 1561d9d660f6SJuergen Gross kfree(tv_nexus); 1562d9d660f6SJuergen Gross return 0; 1563d9d660f6SJuergen Gross } 1564d9d660f6SJuergen Gross 15652eafd729SChristoph Hellwig static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page) 1566d9d660f6SJuergen Gross { 15672eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item); 1568d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1569d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1570d9d660f6SJuergen Gross struct scsiback_nexus *tv_nexus; 1571d9d660f6SJuergen Gross ssize_t ret; 1572d9d660f6SJuergen Gross 1573d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1574d9d660f6SJuergen Gross tv_nexus = tpg->tpg_nexus; 1575d9d660f6SJuergen Gross if (!tv_nexus) { 1576d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1577d9d660f6SJuergen Gross return -ENODEV; 1578d9d660f6SJuergen Gross } 1579d9d660f6SJuergen Gross ret = snprintf(page, PAGE_SIZE, "%s\n", 1580d9d660f6SJuergen Gross tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1581d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1582d9d660f6SJuergen Gross 1583d9d660f6SJuergen Gross return ret; 1584d9d660f6SJuergen Gross } 1585d9d660f6SJuergen Gross 15862eafd729SChristoph Hellwig static ssize_t scsiback_tpg_nexus_store(struct config_item *item, 15872eafd729SChristoph Hellwig const char *page, size_t count) 1588d9d660f6SJuergen Gross { 15892eafd729SChristoph Hellwig struct se_portal_group *se_tpg = to_tpg(item); 1590d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1591d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1592d9d660f6SJuergen Gross struct scsiback_tport *tport_wwn = tpg->tport; 1593d9d660f6SJuergen Gross unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr; 1594d9d660f6SJuergen Gross int ret; 1595d9d660f6SJuergen Gross /* 159678574878STao Chen * Shutdown the active I_T nexus if 'NULL' is passed. 1597d9d660f6SJuergen Gross */ 1598d9d660f6SJuergen Gross if (!strncmp(page, "NULL", 4)) { 1599d9d660f6SJuergen Gross ret = scsiback_drop_nexus(tpg); 1600d9d660f6SJuergen Gross return (!ret) ? count : ret; 1601d9d660f6SJuergen Gross } 1602d9d660f6SJuergen Gross /* 1603d9d660f6SJuergen Gross * Otherwise make sure the passed virtual Initiator port WWN matches 1604d9d660f6SJuergen Gross * the fabric protocol_id set in scsiback_make_tport(), and call 1605d9d660f6SJuergen Gross * scsiback_make_nexus(). 1606d9d660f6SJuergen Gross */ 1607d9d660f6SJuergen Gross if (strlen(page) >= VSCSI_NAMELEN) { 1608d9d660f6SJuergen Gross pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n", 1609d9d660f6SJuergen Gross page, VSCSI_NAMELEN); 1610d9d660f6SJuergen Gross return -EINVAL; 1611d9d660f6SJuergen Gross } 1612d9d660f6SJuergen Gross snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page); 1613d9d660f6SJuergen Gross 1614d9d660f6SJuergen Gross ptr = strstr(i_port, "naa."); 1615d9d660f6SJuergen Gross if (ptr) { 1616d9d660f6SJuergen Gross if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { 1617d9d660f6SJuergen Gross pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n", 1618d9d660f6SJuergen Gross i_port, scsiback_dump_proto_id(tport_wwn)); 1619d9d660f6SJuergen Gross return -EINVAL; 1620d9d660f6SJuergen Gross } 1621d9d660f6SJuergen Gross port_ptr = &i_port[0]; 1622d9d660f6SJuergen Gross goto check_newline; 1623d9d660f6SJuergen Gross } 1624d9d660f6SJuergen Gross ptr = strstr(i_port, "fc."); 1625d9d660f6SJuergen Gross if (ptr) { 1626d9d660f6SJuergen Gross if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { 1627d9d660f6SJuergen Gross pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n", 1628d9d660f6SJuergen Gross i_port, scsiback_dump_proto_id(tport_wwn)); 1629d9d660f6SJuergen Gross return -EINVAL; 1630d9d660f6SJuergen Gross } 1631d9d660f6SJuergen Gross port_ptr = &i_port[3]; /* Skip over "fc." */ 1632d9d660f6SJuergen Gross goto check_newline; 1633d9d660f6SJuergen Gross } 1634d9d660f6SJuergen Gross ptr = strstr(i_port, "iqn."); 1635d9d660f6SJuergen Gross if (ptr) { 1636d9d660f6SJuergen Gross if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { 1637d9d660f6SJuergen Gross pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n", 1638d9d660f6SJuergen Gross i_port, scsiback_dump_proto_id(tport_wwn)); 1639d9d660f6SJuergen Gross return -EINVAL; 1640d9d660f6SJuergen Gross } 1641d9d660f6SJuergen Gross port_ptr = &i_port[0]; 1642d9d660f6SJuergen Gross goto check_newline; 1643d9d660f6SJuergen Gross } 1644d9d660f6SJuergen Gross pr_err("Unable to locate prefix for emulated Initiator Port: %s\n", 1645d9d660f6SJuergen Gross i_port); 1646d9d660f6SJuergen Gross return -EINVAL; 1647d9d660f6SJuergen Gross /* 1648d9d660f6SJuergen Gross * Clear any trailing newline for the NAA WWN 1649d9d660f6SJuergen Gross */ 1650d9d660f6SJuergen Gross check_newline: 1651d9d660f6SJuergen Gross if (i_port[strlen(i_port) - 1] == '\n') 1652d9d660f6SJuergen Gross i_port[strlen(i_port) - 1] = '\0'; 1653d9d660f6SJuergen Gross 1654d9d660f6SJuergen Gross ret = scsiback_make_nexus(tpg, port_ptr); 1655d9d660f6SJuergen Gross if (ret < 0) 1656d9d660f6SJuergen Gross return ret; 1657d9d660f6SJuergen Gross 1658d9d660f6SJuergen Gross return count; 1659d9d660f6SJuergen Gross } 1660d9d660f6SJuergen Gross 16612eafd729SChristoph Hellwig CONFIGFS_ATTR(scsiback_tpg_, nexus); 1662d9d660f6SJuergen Gross 1663d9d660f6SJuergen Gross static struct configfs_attribute *scsiback_tpg_attrs[] = { 16642eafd729SChristoph Hellwig &scsiback_tpg_attr_nexus, 1665d9d660f6SJuergen Gross NULL, 1666d9d660f6SJuergen Gross }; 1667d9d660f6SJuergen Gross 1668d9d660f6SJuergen Gross static ssize_t 16692eafd729SChristoph Hellwig scsiback_wwn_version_show(struct config_item *item, char *page) 1670d9d660f6SJuergen Gross { 1671d9d660f6SJuergen Gross return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on " 1672d9d660f6SJuergen Gross UTS_RELEASE"\n", 1673d9d660f6SJuergen Gross VSCSI_VERSION, utsname()->sysname, utsname()->machine); 1674d9d660f6SJuergen Gross } 1675d9d660f6SJuergen Gross 16762eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(scsiback_wwn_, version); 1677d9d660f6SJuergen Gross 1678d9d660f6SJuergen Gross static struct configfs_attribute *scsiback_wwn_attrs[] = { 16792eafd729SChristoph Hellwig &scsiback_wwn_attr_version, 1680d9d660f6SJuergen Gross NULL, 1681d9d660f6SJuergen Gross }; 1682d9d660f6SJuergen Gross 1683d9d660f6SJuergen Gross static int scsiback_port_link(struct se_portal_group *se_tpg, 1684d9d660f6SJuergen Gross struct se_lun *lun) 1685d9d660f6SJuergen Gross { 1686d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1687d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1688d9d660f6SJuergen Gross 1689d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1690d9d660f6SJuergen Gross tpg->tv_tpg_port_count++; 1691d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1692d9d660f6SJuergen Gross 1693d9d660f6SJuergen Gross return 0; 1694d9d660f6SJuergen Gross } 1695d9d660f6SJuergen Gross 1696d9d660f6SJuergen Gross static void scsiback_port_unlink(struct se_portal_group *se_tpg, 1697d9d660f6SJuergen Gross struct se_lun *lun) 1698d9d660f6SJuergen Gross { 1699d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1700d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1701d9d660f6SJuergen Gross 1702d9d660f6SJuergen Gross mutex_lock(&tpg->tv_tpg_mutex); 1703d9d660f6SJuergen Gross tpg->tv_tpg_port_count--; 1704d9d660f6SJuergen Gross mutex_unlock(&tpg->tv_tpg_mutex); 1705d9d660f6SJuergen Gross } 1706d9d660f6SJuergen Gross 1707d9d660f6SJuergen Gross static struct se_portal_group * 1708aa090eabSBart Van Assche scsiback_make_tpg(struct se_wwn *wwn, const char *name) 1709d9d660f6SJuergen Gross { 1710d9d660f6SJuergen Gross struct scsiback_tport *tport = container_of(wwn, 1711d9d660f6SJuergen Gross struct scsiback_tport, tport_wwn); 1712d9d660f6SJuergen Gross 1713d9d660f6SJuergen Gross struct scsiback_tpg *tpg; 1714495daef9SDan Carpenter u16 tpgt; 1715d9d660f6SJuergen Gross int ret; 1716d9d660f6SJuergen Gross 1717d9d660f6SJuergen Gross if (strstr(name, "tpgt_") != name) 1718d9d660f6SJuergen Gross return ERR_PTR(-EINVAL); 1719495daef9SDan Carpenter ret = kstrtou16(name + 5, 10, &tpgt); 1720495daef9SDan Carpenter if (ret) 1721495daef9SDan Carpenter return ERR_PTR(ret); 1722d9d660f6SJuergen Gross 1723d9d660f6SJuergen Gross tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL); 1724d9d660f6SJuergen Gross if (!tpg) 1725d9d660f6SJuergen Gross return ERR_PTR(-ENOMEM); 1726d9d660f6SJuergen Gross 1727d9d660f6SJuergen Gross mutex_init(&tpg->tv_tpg_mutex); 1728d9d660f6SJuergen Gross INIT_LIST_HEAD(&tpg->tv_tpg_list); 1729d9d660f6SJuergen Gross INIT_LIST_HEAD(&tpg->info_list); 1730d9d660f6SJuergen Gross tpg->tport = tport; 1731d9d660f6SJuergen Gross tpg->tport_tpgt = tpgt; 1732d9d660f6SJuergen Gross 1733bc0c94b1SNicholas Bellinger ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id); 1734d9d660f6SJuergen Gross if (ret < 0) { 1735d9d660f6SJuergen Gross kfree(tpg); 1736d9d660f6SJuergen Gross return NULL; 1737d9d660f6SJuergen Gross } 1738d9d660f6SJuergen Gross mutex_lock(&scsiback_mutex); 1739d9d660f6SJuergen Gross list_add_tail(&tpg->tv_tpg_list, &scsiback_list); 1740d9d660f6SJuergen Gross mutex_unlock(&scsiback_mutex); 1741d9d660f6SJuergen Gross 1742d9d660f6SJuergen Gross return &tpg->se_tpg; 1743d9d660f6SJuergen Gross } 1744d9d660f6SJuergen Gross 1745d9d660f6SJuergen Gross static void scsiback_drop_tpg(struct se_portal_group *se_tpg) 1746d9d660f6SJuergen Gross { 1747d9d660f6SJuergen Gross struct scsiback_tpg *tpg = container_of(se_tpg, 1748d9d660f6SJuergen Gross struct scsiback_tpg, se_tpg); 1749d9d660f6SJuergen Gross 1750d9d660f6SJuergen Gross mutex_lock(&scsiback_mutex); 1751d9d660f6SJuergen Gross list_del(&tpg->tv_tpg_list); 1752d9d660f6SJuergen Gross mutex_unlock(&scsiback_mutex); 1753d9d660f6SJuergen Gross /* 1754d9d660f6SJuergen Gross * Release the virtual I_T Nexus for this xen-pvscsi TPG 1755d9d660f6SJuergen Gross */ 1756d9d660f6SJuergen Gross scsiback_drop_nexus(tpg); 1757d9d660f6SJuergen Gross /* 175878574878STao Chen * Deregister the se_tpg from TCM. 1759d9d660f6SJuergen Gross */ 1760d9d660f6SJuergen Gross core_tpg_deregister(se_tpg); 1761d9d660f6SJuergen Gross kfree(tpg); 1762d9d660f6SJuergen Gross } 1763d9d660f6SJuergen Gross 1764d9d660f6SJuergen Gross static int scsiback_check_true(struct se_portal_group *se_tpg) 1765d9d660f6SJuergen Gross { 1766d9d660f6SJuergen Gross return 1; 1767d9d660f6SJuergen Gross } 1768d9d660f6SJuergen Gross 1769d9d660f6SJuergen Gross static int scsiback_check_false(struct se_portal_group *se_tpg) 1770d9d660f6SJuergen Gross { 1771d9d660f6SJuergen Gross return 0; 1772d9d660f6SJuergen Gross } 1773d9d660f6SJuergen Gross 17749ac8928eSChristoph Hellwig static const struct target_core_fabric_ops scsiback_ops = { 17759ac8928eSChristoph Hellwig .module = THIS_MODULE, 177630c7ca93SDavid Disseldorp .fabric_name = "xen-pvscsi", 1777d9d660f6SJuergen Gross .tpg_get_wwn = scsiback_get_fabric_wwn, 1778d9d660f6SJuergen Gross .tpg_get_tag = scsiback_get_tag, 1779d9d660f6SJuergen Gross .tpg_check_demo_mode = scsiback_check_true, 1780d9d660f6SJuergen Gross .tpg_check_demo_mode_cache = scsiback_check_true, 1781d9d660f6SJuergen Gross .tpg_check_demo_mode_write_protect = scsiback_check_false, 1782d9d660f6SJuergen Gross .tpg_check_prod_mode_write_protect = scsiback_check_false, 1783d9d660f6SJuergen Gross .tpg_get_inst_index = scsiback_tpg_get_inst_index, 1784d9d660f6SJuergen Gross .check_stop_free = scsiback_check_stop_free, 1785d9d660f6SJuergen Gross .release_cmd = scsiback_release_cmd, 1786d9d660f6SJuergen Gross .sess_get_index = scsiback_sess_get_index, 1787d9d660f6SJuergen Gross .sess_get_initiator_sid = NULL, 1788d9d660f6SJuergen Gross .write_pending = scsiback_write_pending, 1789d9d660f6SJuergen Gross .set_default_node_attributes = scsiback_set_default_node_attrs, 1790d9d660f6SJuergen Gross .get_cmd_state = scsiback_get_cmd_state, 1791d9d660f6SJuergen Gross .queue_data_in = scsiback_queue_data_in, 1792d9d660f6SJuergen Gross .queue_status = scsiback_queue_status, 1793d9d660f6SJuergen Gross .queue_tm_rsp = scsiback_queue_tm_rsp, 1794d9d660f6SJuergen Gross .aborted_task = scsiback_aborted_task, 1795d9d660f6SJuergen Gross /* 1796d9d660f6SJuergen Gross * Setup callers for generic logic in target_core_fabric_configfs.c 1797d9d660f6SJuergen Gross */ 1798d9d660f6SJuergen Gross .fabric_make_wwn = scsiback_make_tport, 1799d9d660f6SJuergen Gross .fabric_drop_wwn = scsiback_drop_tport, 1800d9d660f6SJuergen Gross .fabric_make_tpg = scsiback_make_tpg, 1801d9d660f6SJuergen Gross .fabric_drop_tpg = scsiback_drop_tpg, 1802d9d660f6SJuergen Gross .fabric_post_link = scsiback_port_link, 1803d9d660f6SJuergen Gross .fabric_pre_unlink = scsiback_port_unlink, 1804d9d660f6SJuergen Gross 18059ac8928eSChristoph Hellwig .tfc_wwn_attrs = scsiback_wwn_attrs, 18069ac8928eSChristoph Hellwig .tfc_tpg_base_attrs = scsiback_tpg_attrs, 18079ac8928eSChristoph Hellwig .tfc_tpg_param_attrs = scsiback_param_attrs, 1808d9d660f6SJuergen Gross }; 1809d9d660f6SJuergen Gross 1810d9d660f6SJuergen Gross static const struct xenbus_device_id scsiback_ids[] = { 1811d9d660f6SJuergen Gross { "vscsi" }, 1812d9d660f6SJuergen Gross { "" } 1813d9d660f6SJuergen Gross }; 1814d9d660f6SJuergen Gross 181595afae48SDavid Vrabel static struct xenbus_driver scsiback_driver = { 181695afae48SDavid Vrabel .ids = scsiback_ids, 1817d9d660f6SJuergen Gross .probe = scsiback_probe, 1818d9d660f6SJuergen Gross .remove = scsiback_remove, 1819d9d660f6SJuergen Gross .otherend_changed = scsiback_frontend_changed 182095afae48SDavid Vrabel }; 1821d9d660f6SJuergen Gross 1822d9d660f6SJuergen Gross static int __init scsiback_init(void) 1823d9d660f6SJuergen Gross { 1824d9d660f6SJuergen Gross int ret; 1825d9d660f6SJuergen Gross 1826d9d660f6SJuergen Gross if (!xen_domain()) 1827d9d660f6SJuergen Gross return -ENODEV; 1828d9d660f6SJuergen Gross 18299ac8928eSChristoph Hellwig pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n", 18309ac8928eSChristoph Hellwig VSCSI_VERSION, utsname()->sysname, utsname()->machine); 18319ac8928eSChristoph Hellwig 1832d9d660f6SJuergen Gross ret = xenbus_register_backend(&scsiback_driver); 1833d9d660f6SJuergen Gross if (ret) 18342dbcdf33SNicholas Bellinger goto out; 1835d9d660f6SJuergen Gross 18369ac8928eSChristoph Hellwig ret = target_register_template(&scsiback_ops); 1837d9d660f6SJuergen Gross if (ret) 1838d9d660f6SJuergen Gross goto out_unregister_xenbus; 1839d9d660f6SJuergen Gross 1840d9d660f6SJuergen Gross return 0; 1841d9d660f6SJuergen Gross 1842d9d660f6SJuergen Gross out_unregister_xenbus: 1843d9d660f6SJuergen Gross xenbus_unregister_driver(&scsiback_driver); 18442dbcdf33SNicholas Bellinger out: 184578574878STao Chen pr_err("%s: error %d\n", __func__, ret); 1846d9d660f6SJuergen Gross return ret; 1847d9d660f6SJuergen Gross } 1848d9d660f6SJuergen Gross 1849d9d660f6SJuergen Gross static void __exit scsiback_exit(void) 1850d9d660f6SJuergen Gross { 18519ac8928eSChristoph Hellwig target_unregister_template(&scsiback_ops); 1852d9d660f6SJuergen Gross xenbus_unregister_driver(&scsiback_driver); 1853d9d660f6SJuergen Gross } 1854d9d660f6SJuergen Gross 1855d9d660f6SJuergen Gross module_init(scsiback_init); 1856d9d660f6SJuergen Gross module_exit(scsiback_exit); 1857d9d660f6SJuergen Gross 1858d9d660f6SJuergen Gross MODULE_DESCRIPTION("Xen SCSI backend driver"); 1859d9d660f6SJuergen Gross MODULE_LICENSE("Dual BSD/GPL"); 1860d9d660f6SJuergen Gross MODULE_ALIAS("xen-backend:vscsi"); 1861d9d660f6SJuergen Gross MODULE_AUTHOR("Juergen Gross <jgross@suse.com>"); 1862