scsi_tgt_if.c 9.1 KB

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