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