2 * Xen SCSI backend driver
4 * Copyright (c) 2008, FUJITSU Limited
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <linux/list.h>
32 #include <linux/gfp.h>
37 Initialize the translation entry list
39 void scsiback_init_translation_table(struct vscsibk_info *info)
41 INIT_LIST_HEAD(&info->v2p_entry_lists);
42 spin_lock_init(&info->v2p_lock);
47 Add a new translation entry
49 int scsiback_add_translation_entry(struct vscsibk_info *info,
50 struct scsi_device *sdev, struct ids_tuple *v)
53 struct v2p_entry *entry;
54 struct v2p_entry *new;
55 struct list_head *head = &(info->v2p_entry_lists);
58 spin_lock_irqsave(&info->v2p_lock, flags);
60 /* Check double assignment to identical virtual ID */
61 list_for_each_entry(entry, head, l) {
62 if ((entry->v.chn == v->chn) &&
63 (entry->v.tgt == v->tgt) &&
64 (entry->v.lun == v->lun)) {
65 pr_warning("scsiback: Virtual ID is already used. "
66 "Assignment was not performed.\n");
73 /* Create a new translation entry and add to the list */
74 if ((new = kmalloc(sizeof(struct v2p_entry), GFP_ATOMIC)) == NULL) {
75 pr_err("scsiback: %s: kmalloc() error\n", __FUNCTION__);
81 list_add_tail(&new->l, head);
84 spin_unlock_irqrestore(&info->v2p_lock, flags);
90 Delete the translation entry specfied
92 int scsiback_del_translation_entry(struct vscsibk_info *info,
95 struct v2p_entry *entry;
96 struct list_head *head = &(info->v2p_entry_lists);
99 spin_lock_irqsave(&info->v2p_lock, flags);
100 /* Find out the translation entry specified */
101 list_for_each_entry(entry, head, l) {
102 if ((entry->v.chn == v->chn) &&
103 (entry->v.tgt == v->tgt) &&
104 (entry->v.lun == v->lun)) {
109 spin_unlock_irqrestore(&info->v2p_lock, flags);
113 /* Delete the translation entry specfied */
114 scsi_device_put(entry->sdev);
118 spin_unlock_irqrestore(&info->v2p_lock, flags);
124 Perform virtual to physical translation
126 struct scsi_device *scsiback_do_translation(struct vscsibk_info *info,
129 struct v2p_entry *entry;
130 struct list_head *head = &(info->v2p_entry_lists);
131 struct scsi_device *sdev = NULL;
134 spin_lock_irqsave(&info->v2p_lock, flags);
135 list_for_each_entry(entry, head, l) {
136 if ((entry->v.chn == v->chn) &&
137 (entry->v.tgt == v->tgt) &&
138 (entry->v.lun == v->lun)) {
144 spin_unlock_irqrestore(&info->v2p_lock, flags);
150 Release the translation entry specfied
152 void scsiback_release_translation_entry(struct vscsibk_info *info)
154 struct v2p_entry *entry, *tmp;
155 struct list_head *head = &(info->v2p_entry_lists);
158 spin_lock_irqsave(&info->v2p_lock, flags);
159 list_for_each_entry_safe(entry, tmp, head, l) {
160 scsi_device_put(entry->sdev);
165 spin_unlock_irqrestore(&info->v2p_lock, flags);