Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at c9a28fa7b9ac19b676deefa0a171ce7df8755c08 396 lines 9.2 kB view raw
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 32#include <asm/cacheflush.h> 33 34#include "scsi_tgt_priv.h" 35 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 44struct 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 */ 51static struct tgt_ring tx_ring, rx_ring; 52static DECLARE_WAIT_QUEUE_HEAD(tgt_poll_wait); 53 54static 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 62static 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 73static 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 105int scsi_tgt_uspace_send_cmd(struct scsi_cmnd *cmd, u64 itn_id, 106 struct scsi_lun *lun, u64 tag) 107{ 108 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); 109 struct tgt_event ev; 110 int err; 111 112 memset(&ev, 0, sizeof(ev)); 113 ev.p.cmd_req.host_no = shost->host_no; 114 ev.p.cmd_req.itn_id = itn_id; 115 ev.p.cmd_req.data_len = scsi_bufflen(cmd); 116 memcpy(ev.p.cmd_req.scb, cmd->cmnd, sizeof(ev.p.cmd_req.scb)); 117 memcpy(ev.p.cmd_req.lun, lun, sizeof(ev.p.cmd_req.lun)); 118 ev.p.cmd_req.attribute = cmd->tag; 119 ev.p.cmd_req.tag = tag; 120 121 dprintk("%p %d %u %x %llx\n", cmd, shost->host_no, 122 ev.p.cmd_req.data_len, cmd->tag, 123 (unsigned long long) ev.p.cmd_req.tag); 124 125 err = tgt_uspace_send_event(TGT_KEVENT_CMD_REQ, &ev); 126 if (err) 127 eprintk("tx buf is full, could not send\n"); 128 129 return err; 130} 131 132int scsi_tgt_uspace_send_status(struct scsi_cmnd *cmd, u64 itn_id, u64 tag) 133{ 134 struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd); 135 struct tgt_event ev; 136 int err; 137 138 memset(&ev, 0, sizeof(ev)); 139 ev.p.cmd_done.host_no = shost->host_no; 140 ev.p.cmd_done.itn_id = itn_id; 141 ev.p.cmd_done.tag = tag; 142 ev.p.cmd_done.result = cmd->result; 143 144 dprintk("%p %d %llu %u %x\n", cmd, shost->host_no, 145 (unsigned long long) ev.p.cmd_req.tag, 146 ev.p.cmd_req.data_len, cmd->tag); 147 148 err = tgt_uspace_send_event(TGT_KEVENT_CMD_DONE, &ev); 149 if (err) 150 eprintk("tx buf is full, could not send\n"); 151 152 return err; 153} 154 155int scsi_tgt_uspace_send_tsk_mgmt(int host_no, u64 itn_id, int function, 156 u64 tag, struct scsi_lun *scsilun, void *data) 157{ 158 struct tgt_event ev; 159 int err; 160 161 memset(&ev, 0, sizeof(ev)); 162 ev.p.tsk_mgmt_req.host_no = host_no; 163 ev.p.tsk_mgmt_req.itn_id = itn_id; 164 ev.p.tsk_mgmt_req.function = function; 165 ev.p.tsk_mgmt_req.tag = tag; 166 memcpy(ev.p.tsk_mgmt_req.lun, scsilun, sizeof(ev.p.tsk_mgmt_req.lun)); 167 ev.p.tsk_mgmt_req.mid = (u64) (unsigned long) data; 168 169 dprintk("%d %x %llx %llx\n", host_no, function, (unsigned long long) tag, 170 (unsigned long long) ev.p.tsk_mgmt_req.mid); 171 172 err = tgt_uspace_send_event(TGT_KEVENT_TSK_MGMT_REQ, &ev); 173 if (err) 174 eprintk("tx buf is full, could not send\n"); 175 176 return err; 177} 178 179int scsi_tgt_uspace_send_it_nexus_request(int host_no, u64 itn_id, 180 int function, char *initiator_id) 181{ 182 struct tgt_event ev; 183 int err; 184 185 memset(&ev, 0, sizeof(ev)); 186 ev.p.it_nexus_req.host_no = host_no; 187 ev.p.it_nexus_req.function = function; 188 ev.p.it_nexus_req.itn_id = itn_id; 189 if (initiator_id) 190 strncpy(ev.p.it_nexus_req.initiator_id, initiator_id, 191 sizeof(ev.p.it_nexus_req.initiator_id)); 192 193 dprintk("%d %x %llx\n", host_no, function, (unsigned long long)itn_id); 194 195 err = tgt_uspace_send_event(TGT_KEVENT_IT_NEXUS_REQ, &ev); 196 if (err) 197 eprintk("tx buf is full, could not send\n"); 198 199 return err; 200} 201 202static int event_recv_msg(struct tgt_event *ev) 203{ 204 int err = 0; 205 206 switch (ev->hdr.type) { 207 case TGT_UEVENT_CMD_RSP: 208 err = scsi_tgt_kspace_exec(ev->p.cmd_rsp.host_no, 209 ev->p.cmd_rsp.itn_id, 210 ev->p.cmd_rsp.result, 211 ev->p.cmd_rsp.tag, 212 ev->p.cmd_rsp.uaddr, 213 ev->p.cmd_rsp.len, 214 ev->p.cmd_rsp.sense_uaddr, 215 ev->p.cmd_rsp.sense_len, 216 ev->p.cmd_rsp.rw); 217 break; 218 case TGT_UEVENT_TSK_MGMT_RSP: 219 err = scsi_tgt_kspace_tsk_mgmt(ev->p.tsk_mgmt_rsp.host_no, 220 ev->p.tsk_mgmt_rsp.itn_id, 221 ev->p.tsk_mgmt_rsp.mid, 222 ev->p.tsk_mgmt_rsp.result); 223 break; 224 case TGT_UEVENT_IT_NEXUS_RSP: 225 err = scsi_tgt_kspace_it_nexus_rsp(ev->p.it_nexus_rsp.host_no, 226 ev->p.it_nexus_rsp.itn_id, 227 ev->p.it_nexus_rsp.result); 228 break; 229 default: 230 eprintk("unknown type %d\n", ev->hdr.type); 231 err = -EINVAL; 232 } 233 234 return err; 235} 236 237static ssize_t tgt_write(struct file *file, const char __user * buffer, 238 size_t count, loff_t * ppos) 239{ 240 struct tgt_event *ev; 241 struct tgt_ring *ring = &rx_ring; 242 243 while (1) { 244 ev = tgt_head_event(ring, ring->tr_idx); 245 /* do we need this? */ 246 flush_dcache_page(virt_to_page(ev)); 247 248 if (!ev->hdr.status) 249 break; 250 251 tgt_ring_idx_inc(ring); 252 event_recv_msg(ev); 253 ev->hdr.status = 0; 254 }; 255 256 return count; 257} 258 259static unsigned int tgt_poll(struct file * file, struct poll_table_struct *wait) 260{ 261 struct tgt_event *ev; 262 struct tgt_ring *ring = &tx_ring; 263 unsigned long flags; 264 unsigned int mask = 0; 265 u32 idx; 266 267 poll_wait(file, &tgt_poll_wait, wait); 268 269 spin_lock_irqsave(&ring->tr_lock, flags); 270 271 idx = ring->tr_idx ? ring->tr_idx - 1 : TGT_MAX_EVENTS - 1; 272 ev = tgt_head_event(ring, idx); 273 if (ev->hdr.status) 274 mask |= POLLIN | POLLRDNORM; 275 276 spin_unlock_irqrestore(&ring->tr_lock, flags); 277 278 return mask; 279} 280 281static int uspace_ring_map(struct vm_area_struct *vma, unsigned long addr, 282 struct tgt_ring *ring) 283{ 284 int i, err; 285 286 for (i = 0; i < TGT_RING_PAGES; i++) { 287 struct page *page = virt_to_page(ring->tr_pages[i]); 288 err = vm_insert_page(vma, addr, page); 289 if (err) 290 return err; 291 addr += PAGE_SIZE; 292 } 293 294 return 0; 295} 296 297static int tgt_mmap(struct file *filp, struct vm_area_struct *vma) 298{ 299 unsigned long addr; 300 int err; 301 302 if (vma->vm_pgoff) 303 return -EINVAL; 304 305 if (vma->vm_end - vma->vm_start != TGT_RING_SIZE * 2) { 306 eprintk("mmap size must be %lu, not %lu \n", 307 TGT_RING_SIZE * 2, vma->vm_end - vma->vm_start); 308 return -EINVAL; 309 } 310 311 addr = vma->vm_start; 312 err = uspace_ring_map(vma, addr, &tx_ring); 313 if (err) 314 return err; 315 err = uspace_ring_map(vma, addr + TGT_RING_SIZE, &rx_ring); 316 317 return err; 318} 319 320static int tgt_open(struct inode *inode, struct file *file) 321{ 322 tx_ring.tr_idx = rx_ring.tr_idx = 0; 323 324 return 0; 325} 326 327static const struct file_operations tgt_fops = { 328 .owner = THIS_MODULE, 329 .open = tgt_open, 330 .poll = tgt_poll, 331 .write = tgt_write, 332 .mmap = tgt_mmap, 333}; 334 335static struct miscdevice tgt_miscdev = { 336 .minor = MISC_DYNAMIC_MINOR, 337 .name = "tgt", 338 .fops = &tgt_fops, 339}; 340 341static void tgt_ring_exit(struct tgt_ring *ring) 342{ 343 int i; 344 345 for (i = 0; i < TGT_RING_PAGES; i++) 346 free_page(ring->tr_pages[i]); 347} 348 349static int tgt_ring_init(struct tgt_ring *ring) 350{ 351 int i; 352 353 spin_lock_init(&ring->tr_lock); 354 355 for (i = 0; i < TGT_RING_PAGES; i++) { 356 ring->tr_pages[i] = get_zeroed_page(GFP_KERNEL); 357 if (!ring->tr_pages[i]) { 358 eprintk("out of memory\n"); 359 return -ENOMEM; 360 } 361 } 362 363 return 0; 364} 365 366void scsi_tgt_if_exit(void) 367{ 368 tgt_ring_exit(&tx_ring); 369 tgt_ring_exit(&rx_ring); 370 misc_deregister(&tgt_miscdev); 371} 372 373int scsi_tgt_if_init(void) 374{ 375 int err; 376 377 err = tgt_ring_init(&tx_ring); 378 if (err) 379 return err; 380 381 err = tgt_ring_init(&rx_ring); 382 if (err) 383 goto free_tx_ring; 384 385 err = misc_register(&tgt_miscdev); 386 if (err) 387 goto free_rx_ring; 388 389 return 0; 390free_rx_ring: 391 tgt_ring_exit(&rx_ring); 392free_tx_ring: 393 tgt_ring_exit(&tx_ring); 394 395 return err; 396}