Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / scsiback / common.h
1 /*
2  * Copyright (c) 2008, FUJITSU Limited
3  *
4  * Based on the blkback driver code.
5  *
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:
11  * 
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:
18  * 
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  * 
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
28  * IN THE SOFTWARE.
29  */
30
31 #ifndef __SCSIIF__BACKEND__COMMON_H__
32 #define __SCSIIF__BACKEND__COMMON_H__
33
34 #include <linux/module.h>
35 #include <linux/interrupt.h>
36 #include <linux/slab.h>
37 #include <linux/wait.h>
38 #include <linux/sched.h>
39 #include <linux/kthread.h>
40 #include <linux/blkdev.h>
41 #include <linux/list.h>
42 #include <linux/kthread.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_dbg.h>
48 #include <scsi/scsi_eh.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/interface/io/ring.h>
52 #include <xen/interface/io/vscsiif.h>
53
54
55 #define DPRINTK(_f, _a...)                      \
56         pr_debug("(file=%s, line=%d) " _f,      \
57                  __FILE__ , __LINE__ , ## _a )
58
59 struct ids_tuple {
60         unsigned int hst;               /* host    */
61         unsigned int chn;               /* channel */
62         unsigned int tgt;               /* target  */
63         unsigned int lun;               /* LUN     */
64 };
65
66 struct v2p_entry {
67         struct ids_tuple v;             /* translate from */
68         struct scsi_device *sdev;       /* translate to   */
69         struct list_head l;
70 };
71
72 struct vscsibk_info {
73         struct xenbus_device *dev;
74
75         domid_t domid;
76         unsigned int evtchn;
77         unsigned int irq;
78
79         int feature;
80
81         struct vscsiif_back_ring  ring;
82         struct vm_struct *ring_area;
83
84         spinlock_t ring_lock;
85         atomic_t nr_unreplied_reqs;
86
87         spinlock_t v2p_lock;
88         struct list_head v2p_entry_lists;
89
90         struct task_struct *kthread;
91         wait_queue_head_t waiting_to_free;
92         wait_queue_head_t wq;
93         unsigned int waiting_reqs;
94         struct page **mmap_pages;
95
96 };
97
98 typedef struct {
99         unsigned char act;
100         struct vscsibk_info *info;
101         struct scsi_device *sdev;
102
103         uint16_t rqid;
104         
105         uint16_t v_chn, v_tgt;
106
107         uint8_t nr_segments;
108         uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
109         uint8_t cmd_len;
110
111         uint8_t sc_data_direction;
112         uint16_t timeout_per_command;
113         
114         uint32_t request_bufflen;
115         struct scatterlist *sgl;
116         grant_ref_t gref[VSCSIIF_SG_TABLESIZE];
117
118         int32_t rslt;
119         uint32_t resid;
120         uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
121
122         struct list_head free_list;
123 } pending_req_t;
124
125
126
127 #define scsiback_get(_b) (atomic_inc(&(_b)->nr_unreplied_reqs))
128 #define scsiback_put(_b)                                \
129         do {                                            \
130                 if (atomic_dec_and_test(&(_b)->nr_unreplied_reqs))      \
131                         wake_up(&(_b)->waiting_to_free);\
132         } while (0)
133
134 #define VSCSIIF_TIMEOUT         (900*HZ)
135
136 #define VSCSI_TYPE_HOST         1
137
138 irqreturn_t scsiback_intr(int, void *);
139 int scsiback_init_sring(struct vscsibk_info *, grant_ref_t, evtchn_port_t);
140 int scsiback_schedule(void *data);
141
142
143 struct vscsibk_info *vscsibk_info_alloc(domid_t domid);
144 void scsiback_free(struct vscsibk_info *info);
145 void scsiback_disconnect(struct vscsibk_info *);
146 int __init scsiback_interface_init(void);
147 void scsiback_interface_exit(void);
148 int scsiback_xenbus_init(void);
149 void scsiback_xenbus_unregister(void);
150
151 void scsiback_init_translation_table(struct vscsibk_info *info);
152
153 int scsiback_add_translation_entry(struct vscsibk_info *info,
154                         struct scsi_device *sdev, struct ids_tuple *v);
155
156 int scsiback_del_translation_entry(struct vscsibk_info *info,
157                                 struct ids_tuple *v);
158 struct scsi_device *scsiback_do_translation(struct vscsibk_info *info,
159                         struct ids_tuple *v);
160 void scsiback_release_translation_entry(struct vscsibk_info *info);
161
162
163 void scsiback_cmd_exec(pending_req_t *pending_req);
164 void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
165                         uint32_t resid, pending_req_t *pending_req);
166 void scsiback_fast_flush_area(pending_req_t *req);
167
168 void scsiback_rsp_emulation(pending_req_t *pending_req);
169 void scsiback_req_emulation_or_cmdexec(pending_req_t *pending_req);
170 void scsiback_emulation_init(void);
171
172
173 #endif /* __SCSIIF__BACKEND__COMMON_H__ */