scsi_tgt_if.c 9.1 KB

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