scsi_tgt_if.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <asm/cacheflush.h>
  32. #include "scsi_tgt_priv.h"
  33. struct tgt_ring {
  34. u32 tr_idx;
  35. unsigned long tr_pages[TGT_RING_PAGES];
  36. spinlock_t tr_lock;
  37. };
  38. /* tx_ring : kernel->user, rx_ring : user->kernel */
  39. static struct tgt_ring tx_ring, rx_ring;
  40. static DECLARE_WAIT_QUEUE_HEAD(tgt_poll_wait);
  41. static inline void tgt_ring_idx_inc(struct tgt_ring *ring)
  42. {
  43. if (ring->tr_idx == TGT_MAX_EVENTS - 1)
  44. ring->tr_idx = 0;
  45. else
  46. ring->tr_idx++;
  47. }
  48. static struct tgt_event *tgt_head_event(struct tgt_ring *ring, u32 idx)
  49. {
  50. u32 pidx, off;
  51. pidx = idx / TGT_EVENT_PER_PAGE;
  52. off = idx % TGT_EVENT_PER_PAGE;
  53. return (struct tgt_event *)
  54. (ring->tr_pages[pidx] + sizeof(struct tgt_event) * off);
  55. }
  56. static int tgt_uspace_send_event(u32 type, struct tgt_event *p)
  57. {
  58. struct tgt_event *ev;
  59. struct tgt_ring *ring = &tx_ring;
  60. unsigned long flags;
  61. int err = 0;
  62. spin_lock_irqsave(&ring->tr_lock, flags);
  63. ev = tgt_head_event(ring, ring->tr_idx);
  64. if (!ev->hdr.status)
  65. tgt_ring_idx_inc(ring);
  66. else
  67. err = -BUSY;
  68. spin_unlock_irqrestore(&ring->tr_lock, flags);
  69. if (err)
  70. return err;
  71. memcpy(ev, p, sizeof(*ev));
  72. ev->hdr.type = type;
  73. mb();
  74. ev->hdr.status = 1;
  75. flush_dcache_page(virt_to_page(ev));
  76. wake_up_interruptible(&tgt_poll_wait);
  77. return 0;
  78. }
  79. int scsi_tgt_uspace_send_cmd(struct scsi_cmnd *cmd, struct scsi_lun *lun, u64 tag)
  80. {
  81. struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
  82. struct tgt_event ev;
  83. int err;
  84. memset(&ev, 0, sizeof(ev));
  85. ev.p.cmd_req.host_no = shost->host_no;
  86. ev.p.cmd_req.data_len = cmd->request_bufflen;
  87. memcpy(ev.p.cmd_req.scb, cmd->cmnd, sizeof(ev.p.cmd_req.scb));
  88. memcpy(ev.p.cmd_req.lun, lun, sizeof(ev.p.cmd_req.lun));
  89. ev.p.cmd_req.attribute = cmd->tag;
  90. ev.p.cmd_req.tag = tag;
  91. dprintk("%p %d %u %x %llx\n", cmd, shost->host_no,
  92. ev.p.cmd_req.data_len, cmd->tag,
  93. (unsigned long long) ev.p.cmd_req.tag);
  94. err = tgt_uspace_send_event(TGT_KEVENT_CMD_REQ, &ev);
  95. if (err)
  96. eprintk("tx buf is full, could not send\n");
  97. return err;
  98. }
  99. int scsi_tgt_uspace_send_status(struct scsi_cmnd *cmd, u64 tag)
  100. {
  101. struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
  102. struct tgt_event ev;
  103. int err;
  104. memset(&ev, 0, sizeof(ev));
  105. ev.p.cmd_done.host_no = shost->host_no;
  106. ev.p.cmd_done.tag = tag;
  107. ev.p.cmd_done.result = cmd->result;
  108. dprintk("%p %d %llu %u %x\n", cmd, shost->host_no,
  109. (unsigned long long) ev.p.cmd_req.tag,
  110. ev.p.cmd_req.data_len, cmd->tag);
  111. err = tgt_uspace_send_event(TGT_KEVENT_CMD_DONE, &ev);
  112. if (err)
  113. eprintk("tx buf is full, could not send\n");
  114. return err;
  115. }
  116. int scsi_tgt_uspace_send_tsk_mgmt(int host_no, int function, u64 tag,
  117. struct scsi_lun *scsilun, void *data)
  118. {
  119. struct tgt_event ev;
  120. int err;
  121. memset(&ev, 0, sizeof(ev));
  122. ev.p.tsk_mgmt_req.host_no = host_no;
  123. ev.p.tsk_mgmt_req.function = function;
  124. ev.p.tsk_mgmt_req.tag = tag;
  125. memcpy(ev.p.tsk_mgmt_req.lun, scsilun, sizeof(ev.p.tsk_mgmt_req.lun));
  126. ev.p.tsk_mgmt_req.mid = (u64) (unsigned long) data;
  127. dprintk("%d %x %llx %llx\n", host_no, function, (unsigned long long) tag,
  128. (unsigned long long) ev.p.tsk_mgmt_req.mid);
  129. err = tgt_uspace_send_event(TGT_KEVENT_TSK_MGMT_REQ, &ev);
  130. if (err)
  131. eprintk("tx buf is full, could not send\n");
  132. return err;
  133. }
  134. static int event_recv_msg(struct tgt_event *ev)
  135. {
  136. int err = 0;
  137. switch (ev->hdr.type) {
  138. case TGT_UEVENT_CMD_RSP:
  139. err = scsi_tgt_kspace_exec(ev->p.cmd_rsp.host_no,
  140. ev->p.cmd_rsp.tag,
  141. ev->p.cmd_rsp.result,
  142. ev->p.cmd_rsp.len,
  143. ev->p.cmd_rsp.uaddr,
  144. ev->p.cmd_rsp.rw);
  145. break;
  146. case TGT_UEVENT_TSK_MGMT_RSP:
  147. err = scsi_tgt_kspace_tsk_mgmt(ev->p.tsk_mgmt_rsp.host_no,
  148. ev->p.tsk_mgmt_rsp.mid,
  149. ev->p.tsk_mgmt_rsp.result);
  150. break;
  151. default:
  152. eprintk("unknown type %d\n", ev->hdr.type);
  153. err = -EINVAL;
  154. }
  155. return err;
  156. }
  157. static ssize_t tgt_write(struct file *file, const char __user * buffer,
  158. size_t count, loff_t * ppos)
  159. {
  160. struct tgt_event *ev;
  161. struct tgt_ring *ring = &rx_ring;
  162. while (1) {
  163. ev = tgt_head_event(ring, ring->tr_idx);
  164. /* do we need this? */
  165. flush_dcache_page(virt_to_page(ev));
  166. if (!ev->hdr.status)
  167. break;
  168. tgt_ring_idx_inc(ring);
  169. event_recv_msg(ev);
  170. ev->hdr.status = 0;
  171. };
  172. return count;
  173. }
  174. static unsigned int tgt_poll(struct file * file, struct poll_table_struct *wait)
  175. {
  176. struct tgt_event *ev;
  177. struct tgt_ring *ring = &tx_ring;
  178. unsigned long flags;
  179. unsigned int mask = 0;
  180. u32 idx;
  181. poll_wait(file, &tgt_poll_wait, wait);
  182. spin_lock_irqsave(&ring->tr_lock, flags);
  183. idx = ring->tr_idx ? ring->tr_idx - 1 : TGT_MAX_EVENTS - 1;
  184. ev = tgt_head_event(ring, idx);
  185. if (ev->hdr.status)
  186. mask |= POLLIN | POLLRDNORM;
  187. spin_unlock_irqrestore(&ring->tr_lock, flags);
  188. return mask;
  189. }
  190. static int uspace_ring_map(struct vm_area_struct *vma, unsigned long addr,
  191. struct tgt_ring *ring)
  192. {
  193. int i, err;
  194. for (i = 0; i < TGT_RING_PAGES; i++) {
  195. struct page *page = virt_to_page(ring->tr_pages[i]);
  196. err = vm_insert_page(vma, addr, page);
  197. if (err)
  198. return err;
  199. addr += PAGE_SIZE;
  200. }
  201. return 0;
  202. }
  203. static int tgt_mmap(struct file *filp, struct vm_area_struct *vma)
  204. {
  205. unsigned long addr;
  206. int err;
  207. if (vma->vm_pgoff)
  208. return -EINVAL;
  209. if (vma->vm_end - vma->vm_start != TGT_RING_SIZE * 2) {
  210. eprintk("mmap size must be %lu, not %lu \n",
  211. TGT_RING_SIZE * 2, vma->vm_end - vma->vm_start);
  212. return -EINVAL;
  213. }
  214. addr = vma->vm_start;
  215. err = uspace_ring_map(vma, addr, &tx_ring);
  216. if (err)
  217. return err;
  218. err = uspace_ring_map(vma, addr + TGT_RING_SIZE, &rx_ring);
  219. return err;
  220. }
  221. static int tgt_open(struct inode *inode, struct file *file)
  222. {
  223. tx_ring.tr_idx = rx_ring.tr_idx = 0;
  224. return 0;
  225. }
  226. static struct file_operations tgt_fops = {
  227. .owner = THIS_MODULE,
  228. .open = tgt_open,
  229. .poll = tgt_poll,
  230. .write = tgt_write,
  231. .mmap = tgt_mmap,
  232. };
  233. static struct miscdevice tgt_miscdev = {
  234. .minor = MISC_DYNAMIC_MINOR,
  235. .name = "tgt",
  236. .fops = &tgt_fops,
  237. };
  238. static void tgt_ring_exit(struct tgt_ring *ring)
  239. {
  240. int i;
  241. for (i = 0; i < TGT_RING_PAGES; i++)
  242. free_page(ring->tr_pages[i]);
  243. }
  244. static int tgt_ring_init(struct tgt_ring *ring)
  245. {
  246. int i;
  247. spin_lock_init(&ring->tr_lock);
  248. for (i = 0; i < TGT_RING_PAGES; i++) {
  249. ring->tr_pages[i] = get_zeroed_page(GFP_KERNEL);
  250. if (!ring->tr_pages[i]) {
  251. eprintk("out of memory\n");
  252. return -ENOMEM;
  253. }
  254. }
  255. return 0;
  256. }
  257. void scsi_tgt_if_exit(void)
  258. {
  259. tgt_ring_exit(&tx_ring);
  260. tgt_ring_exit(&rx_ring);
  261. misc_deregister(&tgt_miscdev);
  262. }
  263. int scsi_tgt_if_init(void)
  264. {
  265. int err;
  266. err = tgt_ring_init(&tx_ring);
  267. if (err)
  268. return err;
  269. err = tgt_ring_init(&rx_ring);
  270. if (err)
  271. goto free_tx_ring;
  272. err = misc_register(&tgt_miscdev);
  273. if (err)
  274. goto free_rx_ring;
  275. return 0;
  276. free_rx_ring:
  277. tgt_ring_exit(&rx_ring);
  278. free_tx_ring:
  279. tgt_ring_exit(&tx_ring);
  280. return err;
  281. }