2 * SCSI target kernel/user interface functions
4 * Copyright (C) 2005 FUJITA Tomonori <tomof@acm.org>
5 * Copyright (C) 2005 Mike Christie <michaelc@cs.wisc.edu>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 #include <linux/miscdevice.h>
23 #include <linux/file.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tgt.h>
30 #include <scsi/scsi_tgt_if.h>
32 #include "scsi_tgt_priv.h"
36 unsigned long tr_pages[TGT_RING_PAGES];
40 /* tx_ring : kernel->user, rx_ring : user->kernel */
41 static struct tgt_ring tx_ring, rx_ring;
42 static DECLARE_WAIT_QUEUE_HEAD(tgt_poll_wait);
44 static inline void tgt_ring_idx_inc(struct tgt_ring *ring)
46 if (ring->tr_idx == TGT_MAX_EVENTS - 1)
52 static struct tgt_event *tgt_head_event(struct tgt_ring *ring, u32 idx)
56 pidx = idx / TGT_EVENT_PER_PAGE;
57 off = idx % TGT_EVENT_PER_PAGE;
59 return (struct tgt_event *)
60 (ring->tr_pages[pidx] + sizeof(struct tgt_event) * off);
63 static int tgt_uspace_send_event(u32 type, struct tgt_event *p)
66 struct tgt_ring *ring = &tx_ring;
70 spin_lock_irqsave(&ring->tr_lock, flags);
72 ev = tgt_head_event(ring, ring->tr_idx);
74 tgt_ring_idx_inc(ring);
78 spin_unlock_irqrestore(&ring->tr_lock, flags);
83 memcpy(ev, p, sizeof(*ev));
88 flush_dcache_page(virt_to_page(ev));
90 wake_up_interruptible(&tgt_poll_wait);
95 int scsi_tgt_uspace_send_cmd(struct scsi_cmnd *cmd, struct scsi_lun *lun, u64 tag)
97 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
101 memset(&ev, 0, sizeof(ev));
102 ev.p.cmd_req.host_no = shost->host_no;
103 ev.p.cmd_req.data_len = cmd->request_bufflen;
104 memcpy(ev.p.cmd_req.scb, cmd->cmnd, sizeof(ev.p.cmd_req.scb));
105 memcpy(ev.p.cmd_req.lun, lun, sizeof(ev.p.cmd_req.lun));
106 ev.p.cmd_req.attribute = cmd->tag;
107 ev.p.cmd_req.tag = tag;
109 dprintk("%p %d %u %x %llx\n", cmd, shost->host_no,
110 ev.p.cmd_req.data_len, cmd->tag,
111 (unsigned long long) ev.p.cmd_req.tag);
113 err = tgt_uspace_send_event(TGT_KEVENT_CMD_REQ, &ev);
115 eprintk("tx buf is full, could not send\n");
120 int scsi_tgt_uspace_send_status(struct scsi_cmnd *cmd, u64 tag)
122 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
126 memset(&ev, 0, sizeof(ev));
127 ev.p.cmd_done.host_no = shost->host_no;
128 ev.p.cmd_done.tag = tag;
129 ev.p.cmd_done.result = cmd->result;
131 dprintk("%p %d %llu %u %x\n", cmd, shost->host_no,
132 (unsigned long long) ev.p.cmd_req.tag,
133 ev.p.cmd_req.data_len, cmd->tag);
135 err = tgt_uspace_send_event(TGT_KEVENT_CMD_DONE, &ev);
137 eprintk("tx buf is full, could not send\n");
142 int scsi_tgt_uspace_send_tsk_mgmt(int host_no, int function, u64 tag,
143 struct scsi_lun *scsilun, void *data)
148 memset(&ev, 0, sizeof(ev));
149 ev.p.tsk_mgmt_req.host_no = host_no;
150 ev.p.tsk_mgmt_req.function = function;
151 ev.p.tsk_mgmt_req.tag = tag;
152 memcpy(ev.p.tsk_mgmt_req.lun, scsilun, sizeof(ev.p.tsk_mgmt_req.lun));
153 ev.p.tsk_mgmt_req.mid = (u64) (unsigned long) data;
155 dprintk("%d %x %llx %llx\n", host_no, function, (unsigned long long) tag,
156 (unsigned long long) ev.p.tsk_mgmt_req.mid);
158 err = tgt_uspace_send_event(TGT_KEVENT_TSK_MGMT_REQ, &ev);
160 eprintk("tx buf is full, could not send\n");
165 static int event_recv_msg(struct tgt_event *ev)
169 switch (ev->hdr.type) {
170 case TGT_UEVENT_CMD_RSP:
171 err = scsi_tgt_kspace_exec(ev->p.cmd_rsp.host_no,
173 ev->p.cmd_rsp.result,
178 case TGT_UEVENT_TSK_MGMT_RSP:
179 err = scsi_tgt_kspace_tsk_mgmt(ev->p.tsk_mgmt_rsp.host_no,
180 ev->p.tsk_mgmt_rsp.mid,
181 ev->p.tsk_mgmt_rsp.result);
184 eprintk("unknown type %d\n", ev->hdr.type);
191 static ssize_t tgt_write(struct file *file, const char __user * buffer,
192 size_t count, loff_t * ppos)
194 struct tgt_event *ev;
195 struct tgt_ring *ring = &rx_ring;
198 ev = tgt_head_event(ring, ring->tr_idx);
199 /* do we need this? */
200 flush_dcache_page(virt_to_page(ev));
205 tgt_ring_idx_inc(ring);
213 static unsigned int tgt_poll(struct file * file, struct poll_table_struct *wait)
215 struct tgt_event *ev;
216 struct tgt_ring *ring = &tx_ring;
218 unsigned int mask = 0;
221 poll_wait(file, &tgt_poll_wait, wait);
223 spin_lock_irqsave(&ring->tr_lock, flags);
225 idx = ring->tr_idx ? ring->tr_idx - 1 : TGT_MAX_EVENTS - 1;
226 ev = tgt_head_event(ring, idx);
228 mask |= POLLIN | POLLRDNORM;
230 spin_unlock_irqrestore(&ring->tr_lock, flags);
235 static int uspace_ring_map(struct vm_area_struct *vma, unsigned long addr,
236 struct tgt_ring *ring)
240 for (i = 0; i < TGT_RING_PAGES; i++) {
241 struct page *page = virt_to_page(ring->tr_pages[i]);
242 err = vm_insert_page(vma, addr, page);
251 static int tgt_mmap(struct file *filp, struct vm_area_struct *vma)
259 if (vma->vm_end - vma->vm_start != TGT_RING_SIZE * 2) {
260 eprintk("mmap size must be %lu, not %lu \n",
261 TGT_RING_SIZE * 2, vma->vm_end - vma->vm_start);
265 addr = vma->vm_start;
266 err = uspace_ring_map(vma, addr, &tx_ring);
269 err = uspace_ring_map(vma, addr + TGT_RING_SIZE, &rx_ring);
274 static int tgt_open(struct inode *inode, struct file *file)
276 tx_ring.tr_idx = rx_ring.tr_idx = 0;
281 static struct file_operations tgt_fops = {
282 .owner = THIS_MODULE,
289 static struct miscdevice tgt_miscdev = {
290 .minor = MISC_DYNAMIC_MINOR,
295 static void tgt_ring_exit(struct tgt_ring *ring)
299 for (i = 0; i < TGT_RING_PAGES; i++)
300 free_page(ring->tr_pages[i]);
303 static int tgt_ring_init(struct tgt_ring *ring)
307 spin_lock_init(&ring->tr_lock);
309 for (i = 0; i < TGT_RING_PAGES; i++) {
310 ring->tr_pages[i] = get_zeroed_page(GFP_KERNEL);
311 if (!ring->tr_pages[i]) {
312 eprintk("out of memory\n");
320 void scsi_tgt_if_exit(void)
322 tgt_ring_exit(&tx_ring);
323 tgt_ring_exit(&rx_ring);
324 misc_deregister(&tgt_miscdev);
327 int scsi_tgt_if_init(void)
331 err = tgt_ring_init(&tx_ring);
335 err = tgt_ring_init(&rx_ring);
339 err = misc_register(&tgt_miscdev);
345 tgt_ring_exit(&rx_ring);
347 tgt_ring_exit(&tx_ring);