scsi_tgt_if.c 7.7 KB

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