FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SCSI target kernel/user interface functions |
| 3 | * |
| 4 | * Copyright (C) 2005 FUJITA Tomonori <tomof@acm.org> |
| 5 | * Copyright (C) 2005 Mike Christie <michaelc@cs.wisc.edu> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * 02110-1301 USA |
| 21 | */ |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/file.h> |
| 24 | #include <net/tcp.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> |
| 31 | |
akpm@osdl.org | e42ebef | 2006-12-04 23:06:48 -0800 | [diff] [blame] | 32 | #include <asm/cacheflush.h> |
| 33 | |
FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 34 | #include "scsi_tgt_priv.h" |
| 35 | |
FUJITA Tomonori | 930e99b | 2007-02-13 23:21:52 +0900 | [diff] [blame] | 36 | #if TGT_RING_SIZE < PAGE_SIZE |
| 37 | # define TGT_RING_SIZE PAGE_SIZE |
| 38 | #endif |
| 39 | |
| 40 | #define TGT_RING_PAGES (TGT_RING_SIZE >> PAGE_SHIFT) |
| 41 | #define TGT_EVENT_PER_PAGE (PAGE_SIZE / sizeof(struct tgt_event)) |
| 42 | #define TGT_MAX_EVENTS (TGT_EVENT_PER_PAGE * TGT_RING_PAGES) |
| 43 | |
FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 44 | struct tgt_ring { |
| 45 | u32 tr_idx; |
| 46 | unsigned long tr_pages[TGT_RING_PAGES]; |
| 47 | spinlock_t tr_lock; |
| 48 | }; |
| 49 | |
| 50 | /* tx_ring : kernel->user, rx_ring : user->kernel */ |
| 51 | static struct tgt_ring tx_ring, rx_ring; |
| 52 | static DECLARE_WAIT_QUEUE_HEAD(tgt_poll_wait); |
| 53 | |
| 54 | static inline void tgt_ring_idx_inc(struct tgt_ring *ring) |
| 55 | { |
| 56 | if (ring->tr_idx == TGT_MAX_EVENTS - 1) |
| 57 | ring->tr_idx = 0; |
| 58 | else |
| 59 | ring->tr_idx++; |
| 60 | } |
| 61 | |
| 62 | static struct tgt_event *tgt_head_event(struct tgt_ring *ring, u32 idx) |
| 63 | { |
| 64 | u32 pidx, off; |
| 65 | |
| 66 | pidx = idx / TGT_EVENT_PER_PAGE; |
| 67 | off = idx % TGT_EVENT_PER_PAGE; |
| 68 | |
| 69 | return (struct tgt_event *) |
| 70 | (ring->tr_pages[pidx] + sizeof(struct tgt_event) * off); |
| 71 | } |
| 72 | |
| 73 | static int tgt_uspace_send_event(u32 type, struct tgt_event *p) |
| 74 | { |
| 75 | struct tgt_event *ev; |
| 76 | struct tgt_ring *ring = &tx_ring; |
| 77 | unsigned long flags; |
| 78 | int err = 0; |
| 79 | |
| 80 | spin_lock_irqsave(&ring->tr_lock, flags); |
| 81 | |
| 82 | ev = tgt_head_event(ring, ring->tr_idx); |
| 83 | if (!ev->hdr.status) |
| 84 | tgt_ring_idx_inc(ring); |
| 85 | else |
| 86 | err = -BUSY; |
| 87 | |
| 88 | spin_unlock_irqrestore(&ring->tr_lock, flags); |
| 89 | |
| 90 | if (err) |
| 91 | return err; |
| 92 | |
| 93 | memcpy(ev, p, sizeof(*ev)); |
| 94 | ev->hdr.type = type; |
| 95 | mb(); |
| 96 | ev->hdr.status = 1; |
| 97 | |
| 98 | flush_dcache_page(virt_to_page(ev)); |
| 99 | |
| 100 | wake_up_interruptible(&tgt_poll_wait); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int scsi_tgt_uspace_send_cmd(struct scsi_cmnd *cmd, struct scsi_lun *lun, u64 tag) |
| 106 | { |
| 107 | struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); |
| 108 | struct tgt_event ev; |
| 109 | int err; |
| 110 | |
| 111 | memset(&ev, 0, sizeof(ev)); |
| 112 | ev.p.cmd_req.host_no = shost->host_no; |
| 113 | ev.p.cmd_req.data_len = cmd->request_bufflen; |
| 114 | memcpy(ev.p.cmd_req.scb, cmd->cmnd, sizeof(ev.p.cmd_req.scb)); |
| 115 | memcpy(ev.p.cmd_req.lun, lun, sizeof(ev.p.cmd_req.lun)); |
| 116 | ev.p.cmd_req.attribute = cmd->tag; |
| 117 | ev.p.cmd_req.tag = tag; |
| 118 | |
| 119 | dprintk("%p %d %u %x %llx\n", cmd, shost->host_no, |
| 120 | ev.p.cmd_req.data_len, cmd->tag, |
| 121 | (unsigned long long) ev.p.cmd_req.tag); |
| 122 | |
| 123 | err = tgt_uspace_send_event(TGT_KEVENT_CMD_REQ, &ev); |
| 124 | if (err) |
| 125 | eprintk("tx buf is full, could not send\n"); |
| 126 | |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | int scsi_tgt_uspace_send_status(struct scsi_cmnd *cmd, u64 tag) |
| 131 | { |
| 132 | struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); |
| 133 | struct tgt_event ev; |
| 134 | int err; |
| 135 | |
| 136 | memset(&ev, 0, sizeof(ev)); |
| 137 | ev.p.cmd_done.host_no = shost->host_no; |
| 138 | ev.p.cmd_done.tag = tag; |
| 139 | ev.p.cmd_done.result = cmd->result; |
| 140 | |
| 141 | dprintk("%p %d %llu %u %x\n", cmd, shost->host_no, |
| 142 | (unsigned long long) ev.p.cmd_req.tag, |
| 143 | ev.p.cmd_req.data_len, cmd->tag); |
| 144 | |
| 145 | err = tgt_uspace_send_event(TGT_KEVENT_CMD_DONE, &ev); |
| 146 | if (err) |
| 147 | eprintk("tx buf is full, could not send\n"); |
| 148 | |
| 149 | return err; |
| 150 | } |
| 151 | |
| 152 | int scsi_tgt_uspace_send_tsk_mgmt(int host_no, int function, u64 tag, |
| 153 | struct scsi_lun *scsilun, void *data) |
| 154 | { |
| 155 | struct tgt_event ev; |
| 156 | int err; |
| 157 | |
| 158 | memset(&ev, 0, sizeof(ev)); |
| 159 | ev.p.tsk_mgmt_req.host_no = host_no; |
| 160 | ev.p.tsk_mgmt_req.function = function; |
| 161 | ev.p.tsk_mgmt_req.tag = tag; |
| 162 | memcpy(ev.p.tsk_mgmt_req.lun, scsilun, sizeof(ev.p.tsk_mgmt_req.lun)); |
| 163 | ev.p.tsk_mgmt_req.mid = (u64) (unsigned long) data; |
| 164 | |
| 165 | dprintk("%d %x %llx %llx\n", host_no, function, (unsigned long long) tag, |
| 166 | (unsigned long long) ev.p.tsk_mgmt_req.mid); |
| 167 | |
| 168 | err = tgt_uspace_send_event(TGT_KEVENT_TSK_MGMT_REQ, &ev); |
| 169 | if (err) |
| 170 | eprintk("tx buf is full, could not send\n"); |
| 171 | |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | static int event_recv_msg(struct tgt_event *ev) |
| 176 | { |
| 177 | int err = 0; |
| 178 | |
| 179 | switch (ev->hdr.type) { |
| 180 | case TGT_UEVENT_CMD_RSP: |
| 181 | err = scsi_tgt_kspace_exec(ev->p.cmd_rsp.host_no, |
FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 182 | ev->p.cmd_rsp.result, |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 183 | ev->p.cmd_rsp.tag, |
FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 184 | ev->p.cmd_rsp.uaddr, |
FUJITA Tomonori | bc7e380 | 2007-03-03 09:55:54 +0900 | [diff] [blame] | 185 | ev->p.cmd_rsp.len, |
| 186 | ev->p.cmd_rsp.sense_uaddr, |
| 187 | ev->p.cmd_rsp.sense_len, |
FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 188 | ev->p.cmd_rsp.rw); |
| 189 | break; |
| 190 | case TGT_UEVENT_TSK_MGMT_RSP: |
| 191 | err = scsi_tgt_kspace_tsk_mgmt(ev->p.tsk_mgmt_rsp.host_no, |
| 192 | ev->p.tsk_mgmt_rsp.mid, |
| 193 | ev->p.tsk_mgmt_rsp.result); |
| 194 | break; |
| 195 | default: |
| 196 | eprintk("unknown type %d\n", ev->hdr.type); |
| 197 | err = -EINVAL; |
| 198 | } |
| 199 | |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | static ssize_t tgt_write(struct file *file, const char __user * buffer, |
| 204 | size_t count, loff_t * ppos) |
| 205 | { |
| 206 | struct tgt_event *ev; |
| 207 | struct tgt_ring *ring = &rx_ring; |
| 208 | |
| 209 | while (1) { |
| 210 | ev = tgt_head_event(ring, ring->tr_idx); |
| 211 | /* do we need this? */ |
| 212 | flush_dcache_page(virt_to_page(ev)); |
| 213 | |
| 214 | if (!ev->hdr.status) |
| 215 | break; |
| 216 | |
| 217 | tgt_ring_idx_inc(ring); |
| 218 | event_recv_msg(ev); |
| 219 | ev->hdr.status = 0; |
| 220 | }; |
| 221 | |
| 222 | return count; |
| 223 | } |
| 224 | |
| 225 | static unsigned int tgt_poll(struct file * file, struct poll_table_struct *wait) |
| 226 | { |
| 227 | struct tgt_event *ev; |
| 228 | struct tgt_ring *ring = &tx_ring; |
| 229 | unsigned long flags; |
| 230 | unsigned int mask = 0; |
| 231 | u32 idx; |
| 232 | |
| 233 | poll_wait(file, &tgt_poll_wait, wait); |
| 234 | |
| 235 | spin_lock_irqsave(&ring->tr_lock, flags); |
| 236 | |
| 237 | idx = ring->tr_idx ? ring->tr_idx - 1 : TGT_MAX_EVENTS - 1; |
| 238 | ev = tgt_head_event(ring, idx); |
| 239 | if (ev->hdr.status) |
| 240 | mask |= POLLIN | POLLRDNORM; |
| 241 | |
| 242 | spin_unlock_irqrestore(&ring->tr_lock, flags); |
| 243 | |
| 244 | return mask; |
| 245 | } |
| 246 | |
| 247 | static int uspace_ring_map(struct vm_area_struct *vma, unsigned long addr, |
| 248 | struct tgt_ring *ring) |
| 249 | { |
| 250 | int i, err; |
| 251 | |
| 252 | for (i = 0; i < TGT_RING_PAGES; i++) { |
| 253 | struct page *page = virt_to_page(ring->tr_pages[i]); |
| 254 | err = vm_insert_page(vma, addr, page); |
| 255 | if (err) |
| 256 | return err; |
| 257 | addr += PAGE_SIZE; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int tgt_mmap(struct file *filp, struct vm_area_struct *vma) |
| 264 | { |
| 265 | unsigned long addr; |
| 266 | int err; |
| 267 | |
| 268 | if (vma->vm_pgoff) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | if (vma->vm_end - vma->vm_start != TGT_RING_SIZE * 2) { |
| 272 | eprintk("mmap size must be %lu, not %lu \n", |
| 273 | TGT_RING_SIZE * 2, vma->vm_end - vma->vm_start); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | addr = vma->vm_start; |
| 278 | err = uspace_ring_map(vma, addr, &tx_ring); |
| 279 | if (err) |
| 280 | return err; |
| 281 | err = uspace_ring_map(vma, addr + TGT_RING_SIZE, &rx_ring); |
| 282 | |
| 283 | return err; |
| 284 | } |
| 285 | |
| 286 | static int tgt_open(struct inode *inode, struct file *file) |
| 287 | { |
| 288 | tx_ring.tr_idx = rx_ring.tr_idx = 0; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 293 | static const struct file_operations tgt_fops = { |
FUJITA Tomonori | 97f7875 | 2006-11-16 19:24:16 +0900 | [diff] [blame] | 294 | .owner = THIS_MODULE, |
| 295 | .open = tgt_open, |
| 296 | .poll = tgt_poll, |
| 297 | .write = tgt_write, |
| 298 | .mmap = tgt_mmap, |
| 299 | }; |
| 300 | |
| 301 | static struct miscdevice tgt_miscdev = { |
| 302 | .minor = MISC_DYNAMIC_MINOR, |
| 303 | .name = "tgt", |
| 304 | .fops = &tgt_fops, |
| 305 | }; |
| 306 | |
| 307 | static void tgt_ring_exit(struct tgt_ring *ring) |
| 308 | { |
| 309 | int i; |
| 310 | |
| 311 | for (i = 0; i < TGT_RING_PAGES; i++) |
| 312 | free_page(ring->tr_pages[i]); |
| 313 | } |
| 314 | |
| 315 | static int tgt_ring_init(struct tgt_ring *ring) |
| 316 | { |
| 317 | int i; |
| 318 | |
| 319 | spin_lock_init(&ring->tr_lock); |
| 320 | |
| 321 | for (i = 0; i < TGT_RING_PAGES; i++) { |
| 322 | ring->tr_pages[i] = get_zeroed_page(GFP_KERNEL); |
| 323 | if (!ring->tr_pages[i]) { |
| 324 | eprintk("out of memory\n"); |
| 325 | return -ENOMEM; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | void scsi_tgt_if_exit(void) |
| 333 | { |
| 334 | tgt_ring_exit(&tx_ring); |
| 335 | tgt_ring_exit(&rx_ring); |
| 336 | misc_deregister(&tgt_miscdev); |
| 337 | } |
| 338 | |
| 339 | int scsi_tgt_if_init(void) |
| 340 | { |
| 341 | int err; |
| 342 | |
| 343 | err = tgt_ring_init(&tx_ring); |
| 344 | if (err) |
| 345 | return err; |
| 346 | |
| 347 | err = tgt_ring_init(&rx_ring); |
| 348 | if (err) |
| 349 | goto free_tx_ring; |
| 350 | |
| 351 | err = misc_register(&tgt_miscdev); |
| 352 | if (err) |
| 353 | goto free_rx_ring; |
| 354 | |
| 355 | return 0; |
| 356 | free_rx_ring: |
| 357 | tgt_ring_exit(&rx_ring); |
| 358 | free_tx_ring: |
| 359 | tgt_ring_exit(&tx_ring); |
| 360 | |
| 361 | return err; |
| 362 | } |