scsi_tgt_if.c 8.0 KB

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