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